I am new to the PHP world! Currently practicing MySQL and PHP alone
I want to update my <p> content when my query is completed. But seems like my $name only shows the previous value:
$query = $_GET['query'];
$stmt = $conn->prepare("SELECT * FROM wordList WHERE word = '$query'");
$name = ''; // my variable
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
$name == $row['word']; // not working
}
And I am not getting the actual data I want in my HTML:
<p>My name is: <?php echo $name; ?></p>
But getting:
My name is:
Thanks
Try using only one equal to symbol = instead of ==.
$name = $row['word'];
Related
This question already has answers here:
MySQL query pulling one row and displaying first letter of result
(3 answers)
Closed 3 years ago.
<?php
if($_POST)
{
include 'config.php';
$referencenumber=$_POST['reference_number'];
$fullname=$_POST['full_name'];
$sRef=mysqli_real_escape_string($conn,$referencenumber);
$sName=mysqli_real_escape_string($conn,$fullname);
$query="SELECT * From customers where reference_number='$sRef' and
full_name='$sName'";
$result=mysqli_query($conn,$query);
if(mysqli_num_rows($result)==1)
{
session_start();
$_SESSION['id'] = $query['id'];
$_SESSION['refnumber'] = $query['reference_number'];
header('location:index.php');
}
}
?>
The above code is what I have written on my login.php page. After a successful login, the user arrives at the index.php page.
var lz_data = {overwrite:true,111:'<?php
print_r($_SESSION['refnumber'])?>',
I am then attempting to print the reference number on the next page here, which will set a value that matches the one in $_SESSION['refnumber'] but for some reason the output of this code always seems to be 'S' when printing, just the letter S. I get the same output when I try to print $_SESSION['id'] as well. Any ideas?
EDIT: session_start(); has been added at the start of index.php, and the double quotations didn't solve the issue.
You try to get a specific index of a string instead of the result / row array:
$query="SELECT * From customers where reference_number='$sRef' and full_name='$sName'";
$_SESSION['id'] = $query['id']; // $query is a string not an array.
$_SESSION['refnumber'] = $query['reference_number']; // $query is a string not an array.
Why you only get "S" on the session?
You can use a string value on PHP like an array. So you can use $query[0] to get the "S" and $query[1] to get the "E" and so on. In your case the $query['id'] is the same like $query[0] because var_dump('id' == 0) is true.
demo on ideone.com
How to solve this?
You can use the mysqli_result::fetch_array method to get the row with columns as index from $result:
//get the $row from $result with fetch_array.
$row = $result->fetch_array(MYSQLI_ASSOC);
//now you can use the row array to set the values to the session.
$_SESSION['id'] = $row['id'];
$_SESSION['refnumber'] = $row['reference_number'];
So your code looks like the following:
<?php
if($_POST) {
include 'config.php';
$referencenumber = $_POST['reference_number'];
$fullname = $_POST['full_name'];
$sRef = mysqli_real_escape_string($conn, $referencenumber);
$sName = mysqli_real_escape_string($conn, $fullname);
$query = "SELECT * From customers where reference_number='$sRef' and full_name='$sName'";
$result=mysqli_query($conn, $query);
if(mysqli_num_rows($result) == 1) {
//get the $row from $result with fetch_array.
$row = $result->fetch_array(MYSQLI_ASSOC);
session_start();
$_SESSION['id'] = $row['id'];
$_SESSION['refnumber'] = $row['reference_number'];
header('location:index.php');
}
}
I'm trying to execute a statement where it will show all of the results for each row of my table but all it is doing is showing the results from the last row.
<?php
require_once('php/dbconfig.php');
$stmt = $DB_con->prepare("SELECT * FROM users");
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$name = array($row['user_name']);
$email = array($row['user_email']);
}
echo $name[0];
?>
This will work but it will only echo out the name of the last person in my table. If I try to replace the 0 with any other number nothing will show.
I would like to be able to show all users and emails in a list.
In the following block you are assigning each name to the variable $name.
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$name = array($row['user_name']);
$email = array($row['user_email']);
}
I think you intend to either echo $name each iteration of the loop by adding the echo to the loop as:
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$name = $row['user_name'];
$email = $row['user_email'];
echo "{$name} - {$email}";
}
or to add it to an array of names by changing the assignment from $name to $name[] which means 'the next index of the $name array'.
Fetch also returns one record, so you'll need to loop the results or change to fetchAll as suggested by other answers.
Trying to figure out how to do this on multiple records. I am trying things out, looking at reference but not understanding how to get it working, firstly here Here is my code that works:
My href link in loop.
city
My PHP
if (isset($_GET['city'])) {
$city = $_GET['city'];
$stmt = $db->prepare("SELECT * from table WHERE city = ? ");
$stmt->execute(array($city));
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) : //loop starts
} else { //html content }
The above simply pulls city records that are being called via a URL variable in my first loop. What If wanted to repeat this multiple times with different URL Variables to generate different results from the database. So instead of just getting one record like my above code I want to get 2 more, here is a below example of what I mean.
Example Href links in a loop with the URL variables city, name country:
city
name
country
Example of PHP which is not working, but getting these URL variables and passing them through to mysql by SELECT, hopefully you get what I mean):
if (isset($_GET['city'], $_GET['name'], $_GET['country'])) {
$city = $_GET['city'];
$name = $_GET['name'];
$country = $_GET['country'];
$stmt = $db->prepare("SELECT * from table WHERE city = ? OR name = ? OR country = ?");
$stmt->execute(array($city, $name, $country));
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) : //loop starts
} else { //html content }
Any advice on how to achieve this would be appreciated, also if there is an easier way to do this or my current code is not up to scratch, please let me know. Thanks!
if (isset($_GET['city'])||isset($_GET['name'])||isset($_GET['country'])) {
echo $city= $_GET['city'];
echo $name=$_GET['name'];
echo $country $_GET['country'];
}
I think you're looking to put the url's into an array so that you can use them later? If so, you could do something like this:
if (isset($_GET['city'], $_GET['name'], $_GET['country'])) {
$city = $_GET['city'];
$name = $_GET['name'];
$country = $_GET['country'];
//added:
$url_array = array();
$stmt = $db->prepare("SELECT * from table WHERE city = ? OR name = ? OR country = ?");
$stmt->execute(array($city, $name, $country));
//$row changed to $rows
while($rows = $stmt->fetch(PDO::FETCH_ASSOC)) : //loop starts
//foreach added
foreach($rows as $row){
$url_array[] = $row;
}
} else { //html content }
I hope that helps?
The following code is intended to use levenshtein() to compare a user-input word to the values in a column of a MySQL table:
$searched=$_POST['searched'];
$sql = "SELECT * FROM `word_list`;";
$result = mysqli_query($conn,$sql);
while($row=mysqli_fetch_assoc($result))
$title = $row['word'];
$sound = levenshtein($searched, $title);
if ($sound < 4) {
echo $title;
}
?>
My confusion stems from the mechanics of actually looping in the values of the "word" column of the table as a variable for the second string int he levenshtein function.
Ultimately, I'd like to loop the values from that column into the $title variable, and echo the values that produce a levenshtein distance less than 4, but I cannot seem to return any output.
Using a while loop is correct in your example. But you are mixing the mysql and the mysqli extension:
$result = mysqli_query($conn,$sql);
...
while($row=mysql_fetch_assoc($result))
You'll have to use mysqli_fetch_assoc() instead.
Also you are missing a closing } at the end of the while loop. The complete example should look like this:
$searched = $_POST['searched'];
$sql = "SELECT * FROM `word_list`;";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result))
$title = $row['word'];
$sound = levenshtein($searched, $title);
if ($sound < 4) {
echo $title;
}
}
This php is populated by a form posting variables to it. Once it has the car model it should be collecting data from the database and storing each variable for use further down the code. This is used alongside another database so I would rather populate each var list before moving ahead.
The problem is, the database is only collecting the first variable and none of the following variables. How can I call all variables and hold them there for use in the html below.
eg. The client selects the car model, submits and the form sends the model to this code where the database should collect more information about the car for use in a automatically generated PDF brochure.
Thanks for your help!!
//database, table CARS and store variables
$conn = mysql_connect("localhost", "blah", "blah");
mysql_select_db("car_db", $conn);
$sql = "SELECT * FROM cars WHERE modelname = '$carmodel'" or die(mysql_error());
$rs_result = mysql_query ($sql, $conn);
while ($row = mysql_fetch_assoc($rs_result))
//create variables from database entry
$dbbrand = $row['brandname'];
$dbseries = $row['series'];
$dbprice = $row['price'];
$dbseats = $row['seats'];
$dbtype = $row['type'];
$dbcolor = $row['color'];
// -------------------- HTML CONTENT -------------------- //
$html = "
<body>
<!--Print Wrap Start-->
<div id=\"content\">
<!--Header with logo-->
<div id=\"div-head\"><div id=\"div-head\"><span class=\"important\">". $dbbrand ."</span></div></div>
<!--start relative container-->
<div id=\"div-1\"><span class=\"important\">". $dbseries ."</span>
Assuming your syntax is copied as stated above, you need to put braces "{" and "}" around your variable assignments.
$sql = "SELECT * FROM cars WHERE modelname = '$carmodel'" or die(mysql_error());
$rs_result = mysql_query ($sql, $conn);
while ($row = mysql_fetch_assoc($rs_result)) { <----- here
//create variables from database entry
$dbbrand = $row['brandname'];
$dbseries = $row['series'];
$dbprice = $row['price'];
$dbseats = $row['seats'];
$dbtype = $row['type'];
$dbcolor = $row['color'];
} <---- here
The problem is here
while ($row = mysql_fetch_assoc($rs_result))
//create variables from database entry
$dbbrand = $row['brandname'];
That code assigns a record from the result set into the $row variable, which in only used to assign the current value into the $dbbrand variable below the loop.
The while loop only controls one line of code and that is $dbbrand = $row['brandname'];. You need to wrap all the relevant lines of code below as a apart of the the while loop. Below I will demonstrate and correct for the other logical error in your code.
$data = array();
int a=0;
while ($row = mysql_fetch_assoc($rs_result)) {
//create variables from database entry
$data[a]['dbbrand'] = $row['brandname'];
$data[a]['dbseries'] = $row['series'];
$data[a]['dbprice'] = $row['price'];
$data[a]['dbseats'] = $row['seats'];
$data[a]['dbtype'] = $row['type'];
$data[a]['dbcolor'] = $row['color'];
$a++;
}
If you want all values returned by the query, the adjustment to your code above will suffice.