mysql_fetch_array (MYSQL) - mysql

I'm a beginner and I have a error
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program Files\VertrigoServ\www\index.php on line 35
and the code...
$sresult = mysql_query("SELECT code, location FROM banners");
while ($row_s = mysql_fetch_array($sresult))
{
$banner[$row_s["location"]]=$row_s["code"];
}

Try this
$sresult = mysql_query("SELECT code, location FROM banners");
if (!$result) {
die('Invalid query: ' . mysql_error());
}
while ($row_s = mysql_fetch_array($sresult))
{
$banner[$row_s["location"]]=$row_s["code"];
}
And check what the error is.

Something is wrong with the query.
try:
$result = mysql_query("..");
if(!$result){
echo "Query error: " . mysql_error();
}

Related

SQL query and output in PHP does not work - 500 Internal Server Error

i try to put a working SQL query in a PHP-file for my Cronjob. But i receive only a blank 500 Internal Server Error notification.
The SQL query works fine in PHPmyAdmin.
Here the code:
<?php
$con=mysqli_connect("HOST","DBUSER","DBPW","DBNAME");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT oc_order_product.order_id AS bestellnr, oc_order_product.quantity, oc_order_product.model, oc_order_product.name, oc_order.shipping_company, oc_order.shipping_firstname, oc_order.shipping_lastname, oc_order.shipping_city
FROM oc_order_product, oc_order
WHERE oc_order.order_id = oc_order_product.order_id
AND oc_order.order_status_id = 1
ORDER BY bestellnr, model");
while($row = mysqli_fetch_array($result))
{
echo $row['oc_order_product.order_id'] . "; " . $row['oc_order_product.quantity'] "; " . $row['oc_order_product.model'] "; " . $row['oc_order_product.name'] "; " . $row['oc_order.shipping_company'] "; " . $row['oc_order.shipping_firstname'] "; " . $row['oc_order.shipping_lastname'] "; " . $row['oc_order.shipping_city']; //these are the fields that you have stored in your database table
echo "<br />";
}
mysqli_close($con);
?>
The error_log is empty. Is something wrong with my code?
The error message, that i receive via mail ist:
Status: 500 Internal Server Error
X-Powered-By: PHP/5.6.19
Content-type: text/html; charset=UTF-8
Any idea?
Thanx
I can note that this is incorrect:
$row['oc_order_product.order_id']
The row has no column with that name. In fact, you have explicitly given the column the name bestellnr. So, you might try:
$row['bestellnr']
I can't guarantee that is the only problem, though.
That's the final code:
<?php
$pdo = new PDO('mysql:host=DBHOST;dbname=DBNAME', 'DBUSER', 'DBPW');
$sql = "SELECT oc_order_product.order_id AS bestellnr, oc_order_product.quantity, oc_order_product.model, oc_order_product.name, oc_order.shipping_company, oc_order.shipping_firstname, oc_order.shipping_lastname, oc_order.shipping_city
FROM oc_order_product, oc_order
WHERE oc_order.order_id = oc_order_product.order_id
AND oc_order.order_status_id = 1
ORDER BY bestellnr, model";
foreach ($pdo->query($sql) as $row) {
echo $row['bestellnr']."; ".$row['quantity']."; ".$row['model']."; ".$row['name']."; ".$row['shipping_company']."; ".$row['shipping_firstname']."; ".$row['shipping_lastname']."; ".$row['shipping_city']."<br />";
}
?>

mysql_query return error on submit

define('DB_NAME','swiftx');
define('DB_USER','root');
define('DB_PASSWORD','123456');
define('DB_HOST','localhost');
if (isset($_POST['submit'])){
$connection = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD);
if (!$connection) {
die("Database connection failed: " . mysqli_error());
}else{
echo("Database Connected");
}
// 2. Select a database to use
$db_select = mysqli_select_db($connection, DB_NAME);
if (!$db_select) {
die("Database selection failed: " . mysqli_error());
}
$value = $_POST['name'];
$value2 = $_POST['attendance'];
$sql = "INSERT INTO people (Name,Email) VALUES ('$value','$value2')";
if(!mysqli_query($sql)){
die ("ERROR:". mysqli_error()); //error here <<<<<<<<
}
}
mysqli_close($connection);
why is mysql returing error "mysqli_query() expects at least 2 parameters"
i am following a website tutorial , i double check again and again , i have done noting wrong , can anyone help me fix it ? i have no idea what i doing wrong.
for mysqli_query() we need to pass two parameters
$query
$link_identifier
in your case $query is $sql and $link_identifier is $connection..it mean you need to use mysqli_qeury() in this way
mysqli_query($sql, $connection)
Your error "mysqli_query() expects at least 2 parameters" is pretty self-explanatory. It expects two parameters you are giving just one.
Try this:
if(!mysqli_query( $connection, $sql) ){

How to use IF NULL, do this in MySQL?

I'm trying to build a members-system in which you can view certain values from a database.
It worked, so that was great, but I'm having a problem now because I'm using a NULL value on 'age'.
When viewing a profile page, it looks like this:
$id = $_GET["id"];
$result = mysql_query("SELECT * FROM membersfromsite WHERE `idofmember`=$_GET[id]");
$row = mysql_fetch_array($result);
echo "<b>" . $row['userusername'] . "</b>: </p>"; ?>
<?php
$id = $_GET["id"];
$result = mysql_query("SELECT * FROM membersfromsite WHERE `idofmember`=$_GET[id]");
$noage = mysql_query("SELECT ISNULL([age]) FROM membersfromsite WHERE `idofmember`=$_GET[id]");
while ($row = mysql_fetch_array($result))
{
echo "<p class='middle'>id-number: " . $row['idofmember'] . "<br>";
echo "Username: " . $row['userusername'] . "<br>";
if ($noage)
{
echo "Age not specified";
}
else
{
echo "Age: " .$row['age'] ;
}
}
I have tried all kinds of other things, but the problem which I 'm having is that it either returns 'Age not specified' on every userpage or the age on every userpage, including the pages with a NULL value, which makes it look like:
Age:
The code which you can see above returns the age on every page, including the pages with an age which is set to NULL. What I don't understand is if I change the code to this:
$noage = mysql_query("SELECT * FROM membersfromsite WHERE `idofmember`=$_GET[id] AND age IS NULL");
it simply doesn't work. Since I'm using IS NULL instead of = NULL I don't really see why this shouldn't work, but I guess it has to do with the IF which is inside the 'while' thing, I don't really see in what other way I could fix this though...
I'm having an idea what the problem is, because I think that there is already a MyQS, Query done with Results and $noage is maybe ignored because of this, but I don't know how to solve this.
You don't need to do a whole separate $noage query.
Just do:
if(!$row['age'])
{
echo "Age not specified";
}
else
{
echo "Age: " .$row['age'] ;
}
Instead of if($noage) use if(!$row['age']) and skip the second query.
The other reason your code does not work is that the second query returns an array with something like array('expr1' => 0) which is not false. You only get a false result if nothing is found.
The reason why = NULL does not work? There are books written about it, it is just as it is.
Rather than relying on MySQL to tell us if no age was found or not, we can programatically determine whether the age value is Null right in PHP.
Here is an example:
$id = $_GET['id'];
$result = mysql_query("SELECT * FROM membersfromsite WHERE `idofmember` = '" . mysql_real_escape_string($id) . "'");
while($row = mysql_fetch_array($result)) {
echo '<p class='middle'>id-number: ' . $row['idofmember'] . '<br>';
echo 'Username: ' . $row['userusername'] . '<br>';
if($row['age'] === Null) {
echo "Age not specified";
} else {
echo "Age: " .$row['age'] ;
}
}

mysql 5.0 UPDATE syntax

I have been trying to get this code to update, and it just will not work. I've been re-reading it and looking at other examples for hours and need some help to get this to work. It is a basic UPDATE script for a membership table in mysql database. I have mysql version 5.0.91. Nothing I have tried is working. When uploaded and tested in browser, returns echo "update query failed" I bolded the part where it is failing. I just can't find out why. When I check mysqladmin, the table is not updated.
$host="mysqlhost"; // Host name
$username="mysqlusername"; // Mysql username
$password="mysqlpassword"; // Mysql password
$db_name="mydbname"; // Database name
$tbl_name="brothers"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Define $myusername and $mypassword
$mymname=$_POST['mymname'];
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
$myconfirmpassword=$_POST['myconfirmpassword'];
$mysnumber=$_POST['mysnumber'];
$myemail=$_POST['myemail'];
if ($mypassword !== $myconfirmpassword) {
die ("passwords do not match. Try again!");
if (isset($_COOKIE['fname'])) {
$myfname = ($_COOKIE['fname']);
}
else {
die('could not find cookie fname');
}
if (isset($_COOKIE['lname'])) {
$mylname = ($_COOKIE['lname']);
}
else {
die('could not find cookie lname');
}
$sql="SELECT * FROM $tbl_name WHERE fname='$myfname' AND lname='$mylname'";
$result=mysql_query($sql)or die("no sql");
while($row = mysql_fetch_array($result))
{
$fname=$row['fname'];
if (!$fname) {
die('variable not received');
}
$lname=$row['lname'];
$position=$row['position'];
$committee=$row['committee'];}
if($mypassword==$myconfirmpassword) {
$query= "UPDATE brothers
SET `mname`='$mymname' WHERE `fname` ='$fname'";
$chechresult= mysql_query($query) or die (mysql_error());
if (!$checkresult) echo 'update query failed';
elseif ($checkresults) {
echo'update query success';
setcookie('position', $position, time()+86400,'/');
setcookie('committee', $committee, time()+86400,'/');
$headsuccess=header( "location:done_registration.php");
$headsuccess;
if (!$headsuccess) {
die('Could not redirect success registration'); }
}
}
else{
$headlogin=header( "location:error_registration.php");
$headlogin;
if (!$headlogin) {
die('Could not redirect registration error'); }
}
$chechresult= mysql_query($query) or die (mysql_error());
if (!$checkresult) echo 'update query failed';
you misspelled "k" with "h" in "$chechresult="
Try to print $query string that contains the UPDATE order and after execute it in mysql console.
You've misspelled $chechresult - should be $checkresult; also, $fname should be $myfname!
It is probably worth echoing out $query to the screen, and trying that directly in your database - either on your console or in phpMyAdmin. Since $fname is empty (wrong var) your WHERE clause is wrong; at a guess it is affecting zero rows, and so is executing correctly.
Bear in mind that you have some SQL injection security issues in this code - make sure you escape your values before adding them to a query. Better yet, if you upgrade to PDO, you can use parameterisation, which will do the escaping for you.

Can I construct a php page to run a query check against 30 mysql databases?

I host at hostgator and have about 30 mysql databases (all different websites that sit on the same server). For the last year.. no problems and suddenly, the last 2 days, I've seen 5 - 10 of these databases marked as 'crashed' and they return no results... so my websites display no info. I have to run a "repair table mytable" to fix these and then they work great again.
Instead of logging in to go through the databases 1 by 1 every morning, is there a way I could setup a php page to connect to all 30 databases and run a simple select statement.. and if it works, return
"database db1 is working"
"database db2 is working"
and then when not working, return
"no reply from db3"
....or something similar?
Thanks!
There's no reason you couldn't have a script that lists all of your databasenames and login credentials, and try to connect in turn to each:
$logins = array(
array('dbname' => 'blah', 'user' => 'username1', 'password' => 'password1'),
array('dbname' => 'yuck', ....)
...
);
$failures = array();
foreach ($logins as $login) {
$con = mysql_connect('servername', $login['user'], $login['password']);
if (!$con) {
$failures[] = $login['dbname'] . " failed with " . mysql_error();
continue;
}
$result = mysql_select_db($login['dbname']);
if (!$result) {
$failures[] = "Failed to select " . $login['dbname'] . ": " . mysql_error();
continue;
}
$result = mysql_query("SELECT something FROM sometable");
if (!$result) {
$failures[] = "Faile to select from " . $login['dbname'] . ": " . mysql_error();
continue;
}
if (mysql_num_rows($result) != $some_expected_value) {
$failures[] = "Got incorrect rowcount " . mysql_num_rows($result) . " on " . $login['dbname'];
}
etc....
mysql_close();
}
if (count($failures) > 0) {
echo "Failures found: "
print_r($failures);
}
You should be able to do something like the following:
<?php
//connect to database
mysql_connect('database','user','password');
//get all database names
$result = mysql_query("show databases;");
//iterate over all databases returned from 'show databases' query
while($row = mysql_fetch_array($result)) {
//DB name is returned in the result set's first element. select that DB
mysql_selectdb($row[0]);
//get all tables in the database
$query = "show tables;";
$result2 = mysql_query($query);
echo "Query: (".$row[0].")$query\n";
echo mysql_error();
//iterate over all tables in the current database
while($row2 = mysql_fetch_array($result2)) {
//the first element of the returned array will always be the table name, so:
$query = "select * from ".$row2[0]." where 1=1;";
$result3 = mysql_query($query);
echo "Query:\t(".$row[0].'/'.$row2[0].")$query\n";
//If mysql_query returns false (i.e., $result3 is false), that means that
// the table is damaged
if(!$result3) {
echo "***Error on table '".$row2[0]."' *** ... Fixing...";
//So, we repair the table
mysql_query("repair table ".$row2[0].";");
}
}
}
?>