This question already has answers here:
Single result from database using mysqli
(6 answers)
Closed 2 years ago.
I am trying to pull a single value from a database and assign it to a php variable. All of the mysqli functions appear to pull an entire row, while I want one value of that row (ex. ID, name, ect).
This is what I have so far:
$result = mysqli_query($con, "SELECT * FROM test_table WHRE ID='" . $_GET['ID'] . "'");
$row = $result->fetch_assoc();
$test= $row['ID'];
echo $test;
When I run the above I don't get any output; $test is unassigned. What is the correct command to assign a value to my $test variable?
You forgot a E in youy WHERE clause
$result = mysqli_query($con, "SELECT * FROM test_table WHERE ID='" . $_GET['ID'] . "'");
$row = $result->fetch_assoc();
$test= $row['ID'];
echo $test;
If your 'ID' field is a integer, quotes are not necessary.
Related
This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
How can I prevent SQL injection in PHP?
(27 answers)
Closed 2 years ago.
Expected result:
Loop through all entries in the checkedout table, and select the entry from the game table where the barcode field is the same.
Actual behaviour / issue:
For the most part, this is working as intended. If I set the barcode field to a numerical value in the game table, and then "checkout" that barcode, everything works as intended. The barcodes I'll be using are in the format of ABC12345678. Once I change the values in the barcode field, in the game table to the alphanumeric version, it no longer runs the secondary select statement and displays this error: Fatal error: Call to a member function fetch_assoc() on boolean which refers to the following line: while ($row2 = $result2->fetch_assoc()) {
Oddly enough, if I run the exact same select statement SELECT * FROM game WHERE barcode = 'ABC12345678' on the MySQL instance, it returns the proper results.
Question
Do I need to be using a different method to select based on the value now being alphanumeric? Do I need to manipulate the data in some way?
Code:
$sql = "SELECT * FROM checkedout";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$userid = $row["userid"];
$barcode = $row["barcode"];
echo "$userid </br>";
echo "$barcode </br>";
$sql2 = "SELECT * FROM game WHERE barcode = " . $barcode . "";
$result2 = $conn->query($sql2);
while ($row2 = $result2->fetch_assoc()) {
$title = $row2["title"];
$console = $row2["console"];
echo "$title </br>";
echo "$console </br>";
}
checkedout table:
game table:
This question already has answers here:
Get sum of MySQL column in PHP
(9 answers)
Closed 1 year ago.
I'm trying to get the sum off all the prize money from a column in a MySql table, but I'm not getting a result.
$result = mysqli_query("SELECT SUM(prize_money) FROM cards");
while ($rows = mysqli_fetch_array($result)) {
echo $rows['SUM(prize_money)'];
}
I just want to add all of the numbers in the prize_money column then echo the results.
Thank You
You should apply an alias to the SUM so it is easier to access in the PHP.
You then need to pass the connection string to the mysqli_query function as the first parameter.
So for example if your database connection were:
$con=mysqli_connect("localhost","my_user","my_password","my_db");
then you'd use this code to execute the query and assign the alias:
$result = mysqli_query($con, 'SELECT SUM(prize_money) AS sum_prize_money FROM cards');
$row = mysqli_fetch_assoc($result);
$sum = $row['sum_prize_money'];
echo $sum;
Do you get a result if you do this:
$result = mysqli_query("SELECT SUM(prize_money) FROM cards");
$rows = mysqli_fetch_array($result);
echo $rows
As good practice, you should learn to aliase your sql variables e.g. SUM(prize_money) AS total etc
This question already has answers here:
How can I get an unknown username given an ID?
(2 answers)
Closed 12 months ago.
I have a search function on my site which displays results from a MySQL db. Each search result is linked to a dynamic php page with the URL, i.e. /details.php?id=123.
I need the details.php page to get the ID (e.g 123) from the URL and then fetch all rows from the database with this ID, storing them in a variable for use later. I then need to be able to echo the rows at various points throughout the page to populate the content.
The code I have so far is:
<?php
$db = mysql_connect("","","") or die("Database Error");
mysql_select_db("items",$db);
$id = $_GET['id'];
$id = mysql_real_escape_string($id);
$query = "SELECT * FROM `items- table` WHERE `id`='" . $id . "'";
$result = mysql_query($query);
?>
I’m fairly new to PHP so not sure if the code above will get all rows based on the ID, and then how to echo the rows within divs on the page?
You can use mysql_fetch_array() OR mysql_fetch_assoc() function with while loop (if multiple rows there) OR without while loop if there is only one row.
$query = "SELECT * FROM `items- table` WHERE `id`='" . $id . "'";
$result = mysql_query($query);
while($fetch = mysql_fetch_array($result))
{
echo "<div>".$fetch['column_name']."</div>";
}
Almost correct. Just add something like this:
while ($row = mysql_fetch_assoc($result)) {
echo '<div>';
foreach ($row as $key => $val) {
echo $key.' = '.$value.'<br/>';
}
echo '</div>';
}
This question already has answers here:
Single result from database using mysqli
(6 answers)
Closed 4 years ago.
I need to get only the id of member who's username is X from the mysql database.
Can this only be done with a while loop or is there any other way around this?
What I'm thinking of is something like:
$id = mysqli_query($con,'SELECT id FROM membrs WHERE username = '$username' LIMIT 1)
Thanks,
You can use:
mysqli_fetch_array();
// For Instance
$id_get = mysqli_query($con, "SELECT id FROM membrs WHERE username='$username' LIMIT 1");
$id = mysqli_fetch_array($id_get);
echo $id['id']; // This will echo the ID of that user
// What I use is the following for my site:
$user_get = mysqli_query($con, "SELECT * FROM members WHERE username='$username'");
$user = mysqli_fetch_array($user);
echo $user['username']; // This will echo the Username
echo $user['fname']; // This will echo their first name (I used this for a dashboard)
without while loop we can do it by the following code, if you are selecting more than 1 record you need to loop it
$row = mysqli_fetch_array($id);
echo $row['id'];
This question already has answers here:
why this mysql query is not working?
(7 answers)
Closed 8 years ago.
Please help me regarding the specified problem:
The code section:
$result = mysql_query("SELECT *, UNIX_TIMESTAMP(eventdate) AS eventdate,
UNIX_TIMESTAMP(throughdate) AS throughdate FROM events where
id='$_GET[id]' ORDER BY eventdate");
// the above query is not working
if (! $result) {
echo mysql_errno() . ": " . mysql_error(). "\n";
}
if ( mysql_num_rows($result) == 0 ) {
print "<p>No events right now.</p>\n";
}
else {
$lasteventmonth = '';
while ($row = mysql_fetch_array($result)) {
$eventmonth="";
$eventmonth = date("F Y",$row['eventdate']);
if ($lasteventmonth != $eventmonth) {
print "<p style='font-size: 18px;'><b>$eventmonth</b></p>";
}
$lasteventmonth = $eventmonth;
showEvent($row);
}
}
?>
........................
........................//other codes
when the code evaluates as follows:
No events right now.
But specific id is present in the database and if $_GET['id'] is echoed in the page the value is shown.
what is id in id='$_GET[id]' at the beginning?
If you have a query http:// ... ?id=123, I would put id in quotes. Having said that, better like this:
$id = mysql_real_escape_string($_GET['id']); // safe against SQL injection
$sql = "SELECT *, UNIX_TIMESTAMP(eventdate) AS eventdate, UNIX_TIMESTAMP(throughdate) AS throughdate FROM events where id='$id' ORDER BY eventdate";
$result = mysql_query($sql);
If you are still getting trouble, use echo to check the variables $id and $result before the query runs; then you will have a clearer idea why it is not running the query you expect.
I am sure id=$_GET[id] is checking an int versus an int where you have it checking an int vs a string. Remove the single quotes around $_GET['id'] and try again. The single quotes define it as a string rather than an int.