MySQL wrong syntax but no line 114 - php

I am getting the following error
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '' at line 114
but my code is only 27 lines long
<?php
$DB_NAME = 'QCSYSTEM';
$DB_HOST = 'monitor';
$DB_USER = 'QCSYSTEM';
$DB_PASS = '247#Direct';
$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
// A QUICK QUERY ON A FAKE USER TABLE
$query = "SELECT username FROM `users` WHERE";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
// GOING THROUGH THE DATA
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo stripslashes($row['username']);
}
}
else {
echo 'NO RESULTS';
}
// CLOSE CONNECTION
mysqli_close($mysqli);
?>

You are missing to state your WHERE clause here
$query = "SELECT username FROM `users` WHERE";
Either remove it
$query = "SELECT username FROM `users`";
or apply any clause
$query = "SELECT username FROM `users` WHERE column = 'something'";

$query = "SELECT username FROM `users` WHERE";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
You have no where clause specified so the query is failing.

Related

PHP error: Why isn’t this working, I can't see data result in my page?

I'am setting up a new wampserver and created a new database with a table named 'users'
apache 2.4.23 & PHP 7.1.10 & mySQL 5.7.14
<?php
$server = 'localhost';
$serverUsername = 'root';
$serverPassword = '';
$dbName = 'test';
$connection = mysqli_connect($server,$serverUsername,$serverPassword);
msqli_select_db($dbName);
if(!$connection){
echo 'connection failed to database '.mysqli_connect_error();
}
$sql = "SELECT * FROM users";
$query = mysqli_query($sql);
while($row = mysqli_fetch_array($query)){
print_r($row);
}
?>
my value is really in th data base but nothing appears in the page after running code
Have a look at the comments mentioning the fix
$server = 'localhost';
$serverUsername = 'root';
$serverPassword = '';
$dbName = 'test';
// FIX 1
// You need to mention the database name as the last argument
$connection = mysqli_connect($server,$serverUsername,$serverPassword, $dbName);
if(!$connection){
echo 'connection failed to database '.mysqli_connect_error();
}
$sql = "SELECT * FROM users";
// FIX 2
// The first argument should be your mysqli connection
$query = mysqli_query($connection, $sql);
// Check for errors
if (!$query) {
printf("Error: %s\n", mysqli_error($connection));
exit();
}
while($row = mysqli_fetch_array($query)){
print_r($row);
}
?>
Reference for mysqli_connect: https://secure.php.net/manual/en/function.mysqli-connect.php
Reference for mysqli_query: https://secure.php.net/manual/en/mysqli.query.php

PHP: Warning: mysql_fetch_array() expects parameter 1 to be resource

I got this error while adding this code. Would appreciate some help. It's for a CS jackpot site.
$sitename = "website.com"; // YOUR DOMAIN
$link = mysql_connect("localhost", "db_user", "db_pass"); // MYSQL , LOCALHOOST , USERNAME , PASSWORD
$db_selected = mysql_select_db('db_name', $link); // MYSQL DATABASE
mysql_query("SET NAMES utf8");
function fetchinfo($rowname,$tablename,$finder,$findervalue) {
if($finder == "1") $result = mysql_query("SELECT $rowname FROM $tablename");
else $result = mysql_query("SELECT $rowname FROM $tablename WHERE `$finder`='$findervalue'");
while($row = mysql_fetch_assoc($query))
return $row[$rowname];
}
Some tips:
Use mysqli better than mysql
Split the vars in the query, like "SELECT ".$rowname." FROM ".$tablename;
Hope this help...
<?php
//Open a new connection to the MySQL server
$mysqli = new mysqli('host','username','password','database_name');
if ($mysqli->connect_error) {
die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}
//MySqli Select Query
$result = $mysqli->query("SELECT id, product_name FROM products");
while($row = $results->fetch_assoc()) {
echo $row["id"].' - '.$row["product_name"].'<br>';
}
$results->free();
$mysqli->close();

PHP - Trying to get property of non-object in

I'm trying to make this work. I'm simply trying to fill the <option> with the data of the DB from the query.
The error (Trying to get property of non-object in) is on the line of: if ($result3 -> num_rows > 0) {
Here is my code:
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "boleta";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$rut = "<p id='demo'></p>";
$sql3 = "SELECT numero FROM boleta WHERE pago=0 AND persona_rut =". $rut;
$result3 = $conn->query($sql3);
if ($result3 -> num_rows > 0) {
//output data of each row
while($row3 = $result3->fetch_assoc()) {
echo "<option value=". $row3["numero"]. ">" .$row3["numero"]. "</option>";
}
} else {
echo "<option>N/A</option>";
}`
It appears that the statement before this is not correct, where you declare $result3. If your query is coming back with an error, that would cause the PHP error you're getting. Check for an error on that query and it will tell you what the problem is.
If rut is a CHAR or VARCHAR column, you need to put the value in quotes:
$sql3 = "SELECT numero FROM boleta WHERE pago=0 AND persona_rut ='$rut'";
But it would be better to use a prepared statement:
$sql3 = "SELECT numero FROM boleta WHERE pago=0 AND persona_rut = ?";
$stmt = $mysqli->prepare($sql3);
$stmt->bind_param("s", $rut);
$result = $stmt->execute();

php mysql checking if a record exists

I've got problem with checing if a record exists in database. I guess its problem with incorrect using of mysqli_num_rows
<?php
$mysqli = new mysqli("xxxxxx", "xxxxx", "xxxxxx", "xxxxx");
/* check connection */
if ($mysqli->connect_errno) {
die("Connect failed: %s\n" . $mysqli->connect_error);
}
$stmt = $mysqli->prepare("INSERT INTO TEST_CHARS (CHAR_NAME) VALUES(?)");
$stmt->bind_param('s', $nick);
$nick = $_POST['nick'];
$query = mysqli_query("SELECT CHAR_NAME FROM TEST_CHARS WHERE CHAR_NAME ='$nick';");
$count = mysqli_num_rows($query);
if ($count > 0)
{
die('Error, character exist in database');
}
else
{
$stmt->execute();
echo "Character $nick was added successfully";
}
$mysqli->close();
?>
Try with:
$query = mysqli_query($mysqli, "SELECT CHAR_NAME FROM TEST_CHARS WHERE CHAR_NAME ='$nick';");

mysql php error - quote system

I can't get my authors from my php quotes
i have a quotes table:
id, quote, aid
i have a author table:
id, name, etc...
<?php
$DB_SERVER = "localhost";
$DB_USER = "root";
$DB_PASS = "";
$DB_NAME = "test";
$con = mysql_connect($DB_SERVER, $DB_USER, $DB_PASS);
mysql_select_db($DB_NAME);
$sql = mysql_query("SELECT * FROM quotes WHERE id = ".$_GET['id'], $con);
$row = mysql_fetch_row($sql);
$sql = mysql_query("SELECT * FROM author where aid = " . $row[1], $con);
$row = mysql_fetch_row($sql);
var_dump($row);
now i get this error
Warning: mysql_fetch_row() expects parameter 1 to be resource, boolean given in /var/www/domain.com/php.php on line 14
NULL
if you print_r($row); after the first query you will see something like:
Array
(
[0] => id
[1] => quote
[2] => aid
)
then on your second query you use $row[1] which is the quote (string) and not the number.
$sql = mysql_query("SELECT * FROM author where aid = " . $row[1], $con);
if you echo the error (using mysql_error($con)) you will see something:
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'a quote
instead of using mysql_fetch_row use mysql_fetch_assoc and the key of the array will be the name of the column. This way, it's very easy to retrieve data. And don't forget to close your connection.
<?php
$_GET['id'] = 1;
$DB_SERVER = "localhost";
$DB_USER = "root";
$DB_PASS = "";
$DB_NAME = "test";
$con = mysql_connect($DB_SERVER, $DB_USER, $DB_PASS);
mysql_select_db($DB_NAME);
$sql = mysql_query("SELECT * FROM quotes WHERE id = " . (int)$_GET['id'], $con); // or you can use the mysql_real_escape_string
if(!$sql) {
echo mysql_error($con);
}
$row = mysql_fetch_assoc($sql);
mysql_free_result($sql);
$sql = mysql_query("SELECT * FROM author where id = " . (int)$row['aid'], $con);
if(!$sql) {
echo mysql_error($con);
}
$row = mysql_fetch_assoc($sql);
mysql_free_result($sql);
print_r($row);
mysql_close($con);
From the manual:
mysql_query() returns a resource on success, or FALSE on error.
mysql_query() will also fail and return FALSE if the user does not
have permission to access the table(s) referenced by the query.
So just do some quick error checking
$sql = mysql_query("SELECT * FROM author where aid = " . $row[1], $con);
if ( $sql ) {
$row = mysql_fetch_row($sql);
}
else {
//error
}

Categories