i am beginner in php . and i have this Sql problem:
function InsertUserBirdsFromFile($File_content){
for($i =0; $i < count($File_content); $i+=2){
$id = $this->Master_file($File_content[$i], $File_content[$i + 1] );
if(isset($id)){
try{
$qry = "insert into user_to_birds(user_id,tax_id)values( 1 ,'.$id .') ";
$result = mysql_query($qry,$this->connection);
}
catch(Exception $ex){ echo $ex;}
}
}
}
function Master_file($name, $latin ){
try{
$qry = "SELECT tax_id FROM master where name =".$name." and latin =".$latin;
$result = mysql_query($qry,$this->connection);
}
catch(Exception $ex){ return null;}
if ($result == true && mysql_num_rows($result) >0) {
$p=0;
while ($Res_user = mysql_fetch_array($result) ) {
$marques[$p] = $Res_user;
$p++;
}
return $marques[0]['tax_id'];
}
else return null;
}
the error shown is : Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/admin/public_html/hitlist/include/fg_membersite.php on line 427
in this line $result = mysql_query($qry,$this->connection);.
what is the problem? How can i fix it?
Well, maybe unrelated but i think this needs to be fixed
$qry = "insert into user_to_birds(user_id,tax_id)values( 1 ,'.$id .') "
to
$qry = "insert into user_to_birds(user_id,tax_id)values( 1 ,".$id .") "
or
$qry = "insert into user_to_birds(user_id,tax_id)values( 1 ,$id) "
Related
I have a problem switching from MYSQL to MYSQLi. The codes work fine with MYSQL but when i change the connection to MYSQLi, I received the error as stated above when I'm fetching my query. How can i fetch my queries using mysqli functions?
Code:
function __construct(){
$this->link = mysqli_connect('localhost', 'root', '', 'ajax_rating');
if (!$this->link) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
echo 'Success... ' . mysqli_get_host_info($this->link) . "\n";
}
function getItems($id = null){
if(isset($_GET['id']))
{
$query = $this->link->query("SELECT * FROM items WHERE id = '$id'");
}
else
{
$query = $this->link->query("SELECT * FROM items");
}
$rowCount = $query->rowCount();
if($rowCount >= 1)
{
$result = $query->fetchAll();
}
else
{
$result = 0;
}
return $result;
Use num_rows
Read MySQLi row_count
So final answer would be
function getItems($id = null)
{
if(isset($_GET['id']))
{
$query = $this->link->query("SELECT * FROM items WHERE id = '$id'");
}
else
{
$query = $this->link->query("SELECT * FROM items");
}
$rowCount = $query->num_rows;//change here
if($rowCount >= 1)
{
$result = $query->fetchAll();
}
else
{
$result = 0;
}
return $result;
}
EDIT 01
use mysqli_fetch_all instead of fetchAll()
mysqli_fetch_all($query,MYSQLI_ASSOC);
so answer world be
if($rowCount >= 1)
{
$result = mysqli_fetch_all($query,MYSQLI_ASSOC);
}
you can use num_rows for counting rows in DB and you can direct call connection like this :-
//for connection
$con = mysqli_connect("localhost", "root", "", "ajax_rating");
if(!$con)
{
echo "connection error";
}
//query
function getItems($id = null)
{
if(isset($_GET['id']))
{
$query = mysqli_query($con,"SELECT * FROM items WHERE id = '$id'");
}
else
{
$query = mysqli_query($con,"SELECT * FROM items");
}
if (mysqli_num_rows($query) > 0)//change here
{
while($row = mysqli_fetch_assoc($query))
{
$result=$row;
}
}
else
{
$result = 0;
}
return $result;
}
I have to make a web app that gets information from my database, that gets its info from an API). Then I have to show items under certain conditions.
But when I try to add the data from the API, I got a strange message:
Notice: Trying to get property of non-object in c:\xampp\htdocs\IMP03\inleveropdracht3\libs\php\function.php on line 21
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\IMP03\inleveropdracht3\libs\php\function.php on line 21
Here is my PHP code:
<?php
require_once 'settings.php';
$mysqli = mysqli_connect($db_host, $db_user, $db_password, $db_database);
if (mysqli_connect_error()) {
echo mysqli_connect_error($mysqli) . "We are not able to connect to the online database";
}
jsondecode($mysqli);
if (isset($_GET['club']) && !empty($_GET['club'])) {
jsondecode($mysqli);
} else if (isset($_GET['thuisPoint']) && !empty($_GET['thuisPoint']) && ($_GET['uitPoint']) && ($_GET['uitPoint'])) {
updatePoints($mysqli);
} else {
getWedstrijd($mysqli);
}
function jsondecode($mysqli) {
$apiLink = 'http://docent.cmi.hr.nl/moora/imp03/api/wedstrijden?club=';
// $club = $_GET['club'];
$data = json_decode(file_get_contents($apiLink . "Ajax"));
foreach ($data->data as $info) {
$thuisClub = $info->homeClub;
$uitClub = $info->awayClub;
addWestrijden($mysqli, $thuisClub, $uitClub);
}
}
//querys
function addWestrijden($mysqli, $thuisClub, $uitClub) {
$query = "INSERT INTO wedstrijd VALUES(null, '$thuisClub', '$uitClub')";
$resultAddWedstrijd = mysqli_query($mysqli, $query) or die(mysqli_error($mysqli));
getWedstrijd($mysqli);
}
function getWedstrijd($mysqli) {
$query = "SELECT * FROM wedstrijd ORDER BY thuisClub DESC";
$resultGetWedstijd = mysqli_query($mysqli, $query) or die(mysqli_error($mysqli));
while ($result = mysqli_fetch_assoc($resultGetWedstijd)) {
$rows [] = $result;
}
header("Content-Type: application/json");
echo json_encode($rows);
exit;
}
function updatePoints($mysqli) {
$id = $_GET['id'];
$thuisPoints = $_GET['thuisPoint'];
$uitPoints = $_GET['uitPoint'];
$query = "UPDATE wedstrijd "
. "SET thuisPunt = '$thuisPoints', uitPunt = '$uitPoints') "
. "WHERE id = '$id'";
mysqli_query($mysqli, $query) or die(mysqli_error($mysqli));
getWedstrijd($mysqli);
}
I did modify it a bit so it would add data from the API. I really would appreciate it if someone could help me.
Change your foreach to:
foreach ($data as $data => $info)
I Know this is a 'well-known' problem, but I can't get it working. The following MySQL Query (in PHP) gives me this error
$sqle = "UPDATE $gameID SET `$column` = `$vav` WHERE drank='$drank'";
$resulte = mysql_query($sqle) or die('SQL Error (update gegevens):: '.mysql_error());
I tried a lot of different quotes, but I can't get it working. Can someone send me in the right direction?
also;
$column = 'prijs_max';
$vav = $INFO[$count+1]; // returning a number
The complete loop after editing
$count = 0;
foreach ($INFO as $value) {
$column = "";
if(strpos($value, '§') !== false) {
$pieces = explode('§', $value);
$drank = $pieces[0];
$rang = $pieces[1];
if ($rang == 'start') {
$column = 'prijs_start';
} elseif ($rang == 'min') {
$column = 'prijs_min';
} elseif ($rang == 'max') {
$column = 'prijs_max';
}
if ($column == 'prijs_start') {
$bidmaxquery = "SELECT drank FROM $gameID WHERE drank = '$drank'";
$bidmax = mysql_query($bidmaxquery) or die('SQL Error (get drank) :: '.mysql_error());
if (mysql_num_rows($bidmax) == 0) {
$vav = $INFO[$count+1];
$inc = $INFO[$count+7];
$sqld = "INSERT INTO $gameID (drank,$column,prijs_current,increment) VALUES ('$drank','$vav','$vav','$inc')";
$queryd = mysql_query($sqld) or die('SQL Error (insert eerste gegevens):: '.mysql_error());
}
} else {
$vav = $INFO[$count+1];
echo $vav;
echo "<br>";
$sqle = "UPDATE $gameID SET `".$column."`=$vav WHERE drank='$drank'";
$resulte = mysql_query($sqle) or die('SQL Error (update gegevens):: '.mysql_error());
}
}
$count ++;
}
Try
"UPDATE $gameID SET `".$column."`='$vav' WHERE drank='$drank'";
"UPDATE $gameID SET $column ='$vav' WHERE drank='$drank'";
mysql_fetch_array not working in my code :- I got an error like this ...
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program Files\xampp\htdocs\finalreports\generatereport.php on line 38
My code is So far ....
if(array_key_exists('server_guid',$_GET))
{
$guids = $_GET['server_guid'];
$guid_array = explode(",",$guids);
//$reporttype = "Server Resources";
for($i=0 ; $i<count($guid_array); $i++)
{
$query = "select vid from vendor_registration where bussname='".$guid_array[$i]."'";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
$name_array[$i] = $row[0];
}
}
}
Make sure the result is not tainted:
$result = mysql_query($query) or die(mysql_error());
Your query may be returning an error:
if (($result = mysql_query($query)) === false) {
echo "Error running query: " . mysql_error() . "\n";
}
Warning:mysql_fetch_array(): supplied argument is not a valid MySQL result resource in **/home/davzyco1/public_html/notes/functions.php** on line 43
was the error I got when I use the below class, even though the class works PERFECTLY with my old webhost. Here's my new hosts php info: http://davzy.com/notes/php.php
class mysqlDb
{
public $con;
public $debug;
function __construct($host,$username,$password,$database)
{
$this->con = mysql_connect($host,$username,$password);
if (!$this->con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($database, $this->con);
}
function kill()
{
mysql_close($this->con);
}
function debugOn()
{
$this->debug = true;
}
function debugOff()
{
$this->debug = false;
}
function select($query,&$array)
{
$c = 0;
$result = mysql_query("SELECT ".$query);
if($this->debug == true)
echo "SELECT ".$query;
while($row = mysql_fetch_array($result))
{
foreach($row as $id => $value)
{
$array[$c][$id] = $value;
}
$c++;
}
}
function update($update, $where,$array)
{
foreach($array as $id => $value)
{
mysql_query("UPDATE {$update} SET {$id} = '{$value}'
WHERE {$where}");
if($this->debug == true)
echo "UPDATE {$update} SET {$id} = '{$value}'
WHERE {$where}<br><br>";
}
}
function updateModern($update, $where,$array)
{
foreach($array as $id => $value)
{
mysql_query("UPDATE {$update} SET `{$id}` = '{$value}'
WHERE {$where}");
if($this->debug == true)
echo "UPDATE {$update} SET {$id} = '{$value}'
WHERE {$where}<br>";
}
}
function delete($t, $w)
{
mysql_query("DELETE FROM `{$t}` WHERE {$w}");
if($this->debug == true)
echo "DELETE FROM `{$t}` WHERE {$w}<br><br>";
}
function insert($where, $array)
{
$sql = "INSERT INTO `{$where}` (";
$sql2 = " VALUES (";
foreach($array as $id => $value){
$sql .= "`{$id}`, ";
$sql2 .= "'{$value}', ";
}
mysql_query(str_replace(', )',')',$sql.")") . str_replace(', )',')',$sql2.");"));
if($this->debug == true)
echo str_replace(', )',')',$sql.")") . str_replace(', )',')',$sql2.");")."<br><br>";
}
}
This is because mysql_query() will return FALSE if an error occured, instead of returning a result resource. You can check the error by calling mysql_error(), as shown here:
function select($query,&$array)
{
$c = 0;
$result = mysql_query("SELECT ".$query);
if($this->debug == true)
echo "SELECT ".$query;
if (!$result) {
// an error occured, let's see what it was
die(mysql_error());
}
while($row = mysql_fetch_array($result))
{
foreach($row as $id => $value)
{
$array[$c][$id] = $value;
}
$c++;
}
}
Based on the error message, you can find out what the real problem is.
You should really test to see if $result is not false before using it with mysql_fetch_array. The error you're receiving is indicative that the query itself failed.
Have you configured your database with your new host? (do all the tables exist?)
As Mark said above, you really should check your result before trying mysql_fetch_array on a result set, and verify that all tables actually exist.
Without knowing how your original server was set up, I can only guess, but it may also be that your old server was set up to not display warnings.