This is a very simple issue. I must just be doing something stupid:
This query echos out a row id number:
$query = "SELECT * FROM userpage WHERE uploaderrating = $rating";
$result = mysql_query($query);
$row1= mysql_fetch_array($result);
echo $row1[id];
When I add in an additional condition (even though the condition is DEFINITELY met in the SQL database the the echo does produce anything (i.e. the variable is empty). The failing code is:
$query = "SELECT * FROM userpage WHERE uploaderrating = $rating and reviewer = NULL";
$result = mysql_query($query);
$row1= mysql_fetch_array($result);
echo $row1[id];
You can't use
reviewer = NULL
You need to use
reviewer IS NULL
NULL is an undefined value, so it isn't equal to anything; so you need to use IS to look for it.
you should use
$query = "SELECT * FROM userpage WHERE uploaderrating = $rating and reviewer IS NULL";
$result = mysql_query($query);
$row1= mysql_fetch_array($result);
echo $row1[id];
I hope it works.
As far as I know null values are verified with IS
... where xxx IS null
... where xxx IS NOT null
Related
I'm creating a mobile library app, and for one function of the app I am trying to receive the bookID for all books checked out by a certain user. I would like to be able to echo back the results from the query in a string format (preferably with spaces in between each separate book id) so I can deal with the data later on within the app.
Many of the answers I have found online have simply shown how to execute the query, but not how to use the data afterwards. Sorry if this is a simple question to answer, I am a huge novice.
<?php
require "conn.php";
$email = $_POST["email"];
$mysql_qry = "SELECT * FROM user_data WHERE email like '$email'";
$mysql_qry2 = "SELECT DISTINCT(bookID) AS bookID FROM books_checked_out
WHERE userID LIKE $user_id ORDER BY bookID DESC";
$result = mysqli_query($conn, $mysql_qry);
if(mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_assoc($result);
$user_id = $row["user_id"];
$result2 = mysqli_query($conn, $mysqlqry2);
}
else
{
echo "Error, user name not found";
}
$conn->close;
?>
You could append your results into an array and display values using implode():
<?php
require "conn.php";
$email = $_POST["email"]; // You may test here : if (isset($_POST['email']))
$mysql_qry = "SELECT * FROM user_data WHERE email = '$email'";
$result = mysqli_query($conn, $mysql_qry);
if(mysqli_num_rows($result) > 0)
{
$row = mysqli_fetch_assoc($result);
$user_id = $row["user_id"];
$mysql_qry2 = "SELECT DISTINCT(bookID) AS bookID FROM books_checked_out
WHERE userID = $user_id ORDER BY bookID DESC";
$result2 = mysqli_query($conn, $mysql_qry2);
if(mysqli_num_rows($result2) > 0)
{
$ids = [];
while ($row = mysqli_fetch_assoc($result2)) {
$ids[] = $row['bookID'] ;
}
echo implode(" ", $ids) ; // print list of ID
}
else
{
echo "No books checked out!";
}
}
else
{
echo "Error, user name not found";
}
$conn->close;
NB: I used your code here, but, you should have to look to parameterized queries to prevent SQL injections.
Your query $mysql_qry2 should be defined after to get $user_id.
Your LIKE $user_id could be replaced by =.
First thing first, always sanitize your data:
$email = filter_var( $_POST['email'], FILTER_SANITIZE_EMAIL );
$user_id = preg_replace( "#[0-9]#", '', $row['user_id'] );
Use
DISTINCT bookID instead of DISTINCT(bookID)
From your query: $mysql_qry2 = "SELECT DISTINCT(bookID) AS bookID FROM books_checked_out WHERE userID LIKE $user_id ORDER BY bookID DESC";
If you're not getting any result or the returned result is empty but the user_id does exist, then I think the query format is wrong.
What you should do instead
Change the ORDER BY: The query may be correct but mysql returned an empty result because the result order does not match.
Try this
"SELECT DISTINCT bookID AS bookID FROM books_checked_out WHERE userID LIKE $user_id ORDER BY userID DESC";
"SELECT DISTINCT bookID AS bookID FROM books_checked_out WHERE userID LIKE $user_id ORDER BY `primary_key_here` DESC";
Replace <strong>`primary_key_here`</strong> with the primary key name.
Run the query without conditionals and inspect the result
$query = mysqli_query( $conn, "SELECT bookID FROM books_checked_out DESC" );
var_dump( $query );
Use the result to inspect the rest of the query.
Rather than using your own protocol/format use something like JSON or xml in your response to the request.
This will give you better maintainability in the long run and allow you to easily handle the response in the browser with javascript, and most browsers will give you a nice display of JSON objects in the dev console.
You'll have to extract the user id from the result of the first query or you could do a joined query instead.
$email = validate($POST['email']); //where validate() will try to prevent sql injection
//joined query
$query =
" SELECT bookID FROM user_data
INNER JOIN books_checked_out on user_data.user_id = books_checked_out.userID
WHERE user_data.email='$email'
";
//not sure whether that should be user_id or userID looks like you have mixed conventions
//books_checked_out.userID vs user_data.user_id ... check your database column names
//loop through results
// may be empty if user email doesn't exist or has nothing checked out
$result = $conn->query($query);
while($row = $result->fetch_assoc()){
$response[] = ['bookID'=>$row['bookID']];
}
echo json_encode($response);
When receiving the result in php you can use json_decode() or in javascript/ajax it will automatically be available in your result variable.
if things aren't working as expected it can be a good idea to echo the actual sql. In this case
echo 'SQL IS: '.$query;
and test it against your database directly (phpmyadmin/MySQL-Workbench) to see if you get any results or errors.
In the following PHP code, s_at is a column that includes integer values in database. I need to declare one of that value where index is 1, to $s_at. But won't happen in this way. I'm glad if anyone could help.
$s_at = mysql_query("Select('s_at') FROM `time_holder` WHERE `index` = 1");
quite easy:
$mi_resource = mysql_query("Select `s_at` FROM `time_holder` WHERE `index` = 1");
// .... error checking left out
$records = mysql_fetch_assoc($mi_resource);
// .... error checking left out
$s_at = $records[$record_num]['s_at']; // probably #0 if only one record.
...
see mysql_query
Change your code to this and it will work for sure :
$result = mysql_query("Select `s_at` FROM `time_holder` WHERE `index` = 1");
$row = mysql_fetch_array($result);
$s_at = $row['s_at'];
I have the code below and I just want to count from the table members how many people have a 1 in the column loggedin and echo that back. I'm sure I'm missing something small, I just can't see it.
<?php
include ('functions.php');
connect();
$result = mysql_query("SELECT * FROM members WHERE loggedin = '1'");
$num_rows = mysql_num_rows($result);
$total_mem = $num_rows + (1223);
return $total_mem;
echo $total_mem;
?>
The echo will never be called because it is after the return statement.
Remove the return statement and the value should be shown.
Why not let your database do the counting for you?
$result = mysql_query("SELECT count('id') as logged_in_count FROM members WHERE loggedin = '1'");
$row = mysql_fetch_assoc($result);
$num_rows = $row['logged_in_count'];
$total_mem = $num_rows + (1223);
echo $total_mem;
return $total_mem;
You're never going to hit that echo statement, because you have a return statement right above it.
Why not use SELECT COUNT(1) FROM members WHERE loggedin = 1, and then pull the value directly from that? You'll save time because it will only need to return 1 row instead of all the rows, when all you want is the count.
I've got the following query in an existing script - but it's not always returning a value even though it should based off what's in the database. There are plenty of things in the database it SHOULD be grabbing - they are there.
Don't see anything wrong with it - but I barely do this anymore :) See anything?
$query = "SELECT id FROM xtags WHERE tag_id = '$tagid' ORDER BY RAND() Limit 2";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$query = "SELECT * FROM xtable WHERE id = '$row[id]'";
$result = mysql_query($query) or die(mysql_error());
$row2 = mysql_fetch_assoc($result);
echo $row2[title];
}
$result is being used inside the loop and outside, try making a new variable inside and not reusing the outside one.
You're reusing the $result variable inside the loop which overwrites the value for use in the while condition. Use a different name for $query and $result inside the loop.
I don't know if is ok, but you are using $result twice, one before the "while" and another inside the "while".
I would personally split the string and the variable $row.
Why not use var_dump() to see $row and the other variables ???
I don't understand what you are trying to do actually, but try this:
$query = "SELECT id FROM xtags WHERE tag_id = '".$tagid."' ORDER BY RAND() Limit 2";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$query2 = "SELECT * FROM xtable WHERE id = '".intval($row[id])."'";
$result2 = mysql_query($query2) or die(mysql_error());
$row2 = mysql_fetch_assoc($result2);
echo $row2[title];
}
Problem solved - did a join statement.
I have the following code and it should return just one value (id) from mysql table. The following code doesnt work. How can I output it without creating arrays and all this stuff, just a simple output of one value.
$query = "SELECT id FROM users_entity WHERE username = 'Admin' ";
$result = map_query($query);
echo $result;
I do something like this:
<?php
$data = mysql_fetch_object($result);
echo $data->foo();
?>
You have to do some form of object creation. There's no real way around that.
You can try:
$query = "SELECT id FROM users_entity WHERE username = 'Admin' ";
//$result = map_query($query);
//echo $result;
$result = mysql_query($query); // run the query and get the result object.
if (!$result) { // check for errors.
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($result); // get the single row.
echo $row['id']; // display the value.
all you have is a resource, you would still have to make it construct a result array if you want the output.
Check out ADO if you want to write less.
Not sure I exactly understood, what you want, but you could just do
$result = mysql_query('SELECT id FROM table WHERE area = "foo" LIMIT 1');
list($data) = mysql_fetch_assoc($result);
if you wish to execute only one row you can do like this.
$query = "SELECT id FROM users_entity WHERE username = 'Admin' ";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
echo $row[0];
there have been many ways as answered above and this is just my simple example. it will echo the first row that have been executed, you can also use another option like limit clause to do the same result as answered by others above.