Query would run directly on MySQL but not through PHP - php

I have a simple MySQL query
select * from tutor where verified = 0 and alert_by < '2015-08-05' LIMIT 0,1
Now, running this directly through phpMyAdmin provides the desired results, however, when this query is being executed through a set of PHP statements, it doesn't return anything. Below is my code in PHP
$this_date = date("Y-m-d");
$query = "select * from tutor where verified = 0 and alert_by < '$this_date' LIMIT 0,1";
$contact = mysqli_query($conn, $query);
$row = $contact->fetch_array(MYSQLI_ASSOC);
However, the $row is empty, I can't seem to figure this out. I know this seems trivial, but its a little annoying.
Note: Removing "and alert_by < '$this_date'" from the query, works fine.

Check
Connection
$con = mysqli_connect("localhost","my_user","my_password","my_db");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
Use mysqli_query like this
$contact = mysqli_query($con,"select * from tutor where verified = 0 and alert_by < '$this_date' LIMIT 0,1");
Use fetch array like this (Example #2 Procedural style)
$row = mysqli_fetch_array($contact, MYSQLI_ASSOC)

Related

LIMIT doesn't work in MariaDB - Tried a lot of things

$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "igscript";
$con = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
$query = "SELECT * FROM lastsearches";
$result = mysqli_query($con, $query);
if(mysqli_connect_errno()) {
die("DB Error: " . mysqli_connect_error() . " ( " .mysqli_connect_errno() . ")");
}
while($row = mysqli_fetch_assoc($result)) {
$query = "SELECT * FROM lastsearches Order By data DESC LIMIT 1;";
echo '<center><p>'.$row["name"].'</p> </center><hr>';
if(!mysqli_query($con, $query)) {
die('Error : '.mysqli_error($con));
}
}
$result = mysqli_query($con, $query);
Whenever i use either LIMIT 1, or LIMIT 10; at $query, it has no effect at all. Still displays the same amount of rows. I tried also TOP 10 or TOP(10) as I seen on internet, and i'm getting
Error : You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '10 name FROM lastsearches Order By data DESC LIMIT 1' at line 1
$query = "SELECT TOP 10 name FROM lastsearches Order By data DESC";
-> this was the query;
Also the first query worked properly in phpmyadmin, section SQL.
The query from which you are actually displaying results is this one:
$query = "SELECT * FROM lastsearches";
$result = mysqli_query($con, $query);
If you want to limit the results, you need to edit that query instead i.e.
$query = "SELECT * FROM lastsearches Order By data DESC LIMIT 1";
$result = mysqli_query($con, $query);
I'm not sure what you are trying to achieve with the query in your while loop, but it is not doing anything inside that loop so you can probably remove it.
TOP 10 is SQL Server syntax, not MySQL. MySQL uses LIMIT 10 with a very similar effect.
Since I don't see TOP 10 in your code, could it be some interface package that is tied to SQL Server?

Double while loop with sql query

I created a code that is working fine but I'm not sure if its 'legit'.
I am using a sql query in a while loop from another sql query, that means that the (second) sql query is repeated the amount of rows the first query returns.
Can anyone tell me if I can use this or its just one complete mess?
the code:
$conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql_feat = 'SELECT * FROM wp_wpl_dbst WHERE category = 105';
$result_feat = mysqli_query($conn, $sql_feat);
if (mysqli_num_rows($result_feat) > 0) {
while($row = mysqli_fetch_assoc($result_feat)) {
$filter_feat = $row["table_column"];
$filter_name = $row["name"];
$sql_feat2 = 'SELECT * FROM wp_wpl_properties';
$result_feat2 = mysqli_query($conn, $sql_feat2);
if (mysqli_num_rows($result_feat2) > 0) {
while($row2 = mysqli_fetch_assoc($result_feat2)) {
if (!empty($row2[$filter_feat])) {
echo $filter_name;
echo "<br>";
}
}
}
}
}
mysqli_close($conn);
First, you should always check the result of mysqli_query before assuming it represents a proper query result:
// you should check if this value is FALSE before using it
$result_feat = mysqli_query($conn, $sql_feat);
// and this too
$result_feat2 = mysqli_query($conn, $sql_feat2);
As for your code running a query inside the loop, it is pretty much always discouraged unless there is no other option. You should explore the possibility of a JOIN before running any queries inside a loop. Cases may exist where you have no other option, but in your code, I don't see any point at all to run this query over and over:
$sql_feat2 = 'SELECT * FROM wp_wpl_properties';
If it made reference to the value from $row, then maybe. But it doesn't.
Essentially you are doing dynamic sql so join would not be possible but adding where clause to second select would make it more efficient. something like - $sql_feat2 = 'SELECT ' + $filter_name + ' FROM wp_wpl_properties where ' + $row2[$filter_feat] + ' is not null';

Count instances of mysql database

I need to count the number of instances of a word in a column from my database and then get a true or false result depending on if it exceeds 36 instances. I use wordpress and I know that the connecction to the database is correct. And I am using wordpress.
This is what I got this far but its not working:
$selected = mysql_select_db("bringes_phpbb3", $dbConn) or die("Could not select database. The error was ".mysql_error());
mysql_query("SET NAMES utf8");
$SQL_COUNT ="SELECT COUNT(field_name) AS total_number FROM cyklister WHERE grupp LIKE CONCAT ('%','word','%')";
$result = mysql_query($SQL_COUNT);
if ($result >= 36){
$awnser = true;
mysql_free_result($result);
mysql_close($dbConn);
The SQL query is not complete or may be far from what I am looking for. Can someone help me?
After executing mysql_query, you need to call mysql_fetch_array to retrieve the result:
$result = mysql_query($SQL_COUNT);
$row = mysql_fetch_array($result);
$count = $row[0];
if ($count >= 36) {
$awnser = true;
}

mysqli_query() returns empty results for full table

I am making a page that queries a table for all columns of all results ordered by entry time in descending order with a limit. When I query for a count of the rows, the query works just fine, but when I try to query the table again for data, I don't get anything. I decided to try cutting the query down to "SELECT * FROM comments" but I still got no results when "SELECT COUNT(*) AS count FROM comments" just beforehand worked. I've tried using mysqli_error(), but that didn't give me any information.
The query doesn't seem to be failing as the result from mysqli_query() isn't false and when I query in phpMyAdmin, the queries work. A little piece of my code below
//open databases
require_once($root . "databases/data.php");
//get number of suggestions in comments table
$cquery = mysqli_query($cbase, "SELECT COUNT(*) AS count FROM comments"); //this works
$c = mysqli_fetch_array($cquery);
$count = $c["count"];
//get all suggestions
//this query fails
$queryText = "SELECT * FROM comments ORDER BY time DESC LIMIT " . (($page - 1) * $pageLimit) . ", " . $pageLimit;
$query = mysqli_query($cbase, $queryText);
//validate query
if($query === false)
{
$failed = true;
}
//get all comments from query
while(!$failed && $array = mysqli_fetch_array($result))
Please try this on line 3
$c = mysqli_fetch_assoc($cquery);
You can also try like this also,
$c = mysqli_fetch_array($cquery, MYSQLI_ASSOC);
You are just using the wrong variable when reading out your query results in your while-loop. mysqli_fetch_array($result) while you saved the query-result in $query so it should be mysqli_fetch_array($query)

MySQL not getting query, not displaying any errors

Im trying to run a MySQL query, but for some reason its not working.
This is the query im running:
SELECT * FROM applications WHERE status = 0
And my database looks like this:
http://gyazo.com/cc0bfa109e73a771d99a22b5051ee2de
However, num_rows() returns 0 rows...
No errors are displayed...
Try below code hope it will help you because i have tested in localhost.
$sql = "SELECT * FROM applications WHERE status = 0";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo $row['mcUser'];
}
IF its not work's check your table name again.
replace the query with single quotes between 0
$sql = "SELECT * FROM applications WHERE status = '0'";

Categories