MySQL not a valid ressource - php

Output:
Warning: mysql_query(): 6 is not a valid MySQL-Link resource in C:...\mysql_helper.php on line 94
$con = mysql_connect($GLOBALS['mysql_host'], $GLOBALS['mysql_username'], $GLOBALS['mysql_password']) or die(mysql_error());
$db = mysql_select_db($GLOBALS['mysql_database']) or die(mysql_error($con));
$username=sanitize_mysql($username);
$password=sanitize_mysql($password);
$email=sanitize_mysql($email);
if(check_exists("users", "username", $username) == FALSE){
$query = "INSERT INTO users VALUES('".$username."','".$password."','".$email."','".$status."','".$reg_date."','".$own_ref_id."','')";
$result = mysql_query($query,$con) or die(mysql_error($con));
return TRUE;
} else {
return FALSE;
}
mysql_close($con);
Works in every other function built like this (copy/paste)
This is check_exists
function check_exists($table,$specifier,$value)
{
$con = mysql_connect($GLOBALS['mysql_host'], $GLOBALS['mysql_username'], $GLOBALS['mysql_password']) or die(mysql_error());
$db = mysql_select_db($GLOBALS['mysql_database']) or die(mysql_error($con));
$query = "SELECT * FROM ".$table." WHERE ".$specifier." = '".$value."'";
$erg = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($erg)) {
mysql_close($con);
return TRUE;
}
mysql_close($con);
return FALSE;
}

It appears that the $con in check_exists() is in the same scope as your $con and, therefore, check_exists() first overrides (and loses) your original connection then subsequently closes its own connection when it calls mysql_close($con).
You would be better to maintain a single connection that is left open for the use of all such functions.

Related

Table 'databasename.info' doesn't exist

So I installed this jackpot script with a layout and everything and within the jackpot script there was a set.php file which I tried to set up, it looked like this:
<?php
$sitename = "csgoxd.net";
$link = #mysql_connect("localhost:3306", "csgoxdne", "thisisasecretpassword");
$db_selected = mysql_select_db('csgoxdne_csgoxddb', $link);
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'") or die (mysql_error());
$row = mysql_fetch_assoc($result);
return $row[$rowname];
}
?>
So I'm new when it comes to coding in general (I know some basic stuff but that's it) so basically I'm not sure if I'm supposed to fill out more of this file because I get this error on my website.
"Table 'csgoxdne_csgoxddb.info' doesn't exist"
I'm new to this and I'm trying to learn so help is much appreciated.
You should use MySQLi to make use of its advantages it offers over MySQL. You can see more here.
The script you have isn't all too bad, but it does need some tweaking. It's vulnerable to injection like Marc B said. I'm going to assume that csgoxdne_csgoxddb is your table name.
Try this:
<?php
$mysqli = new mysqli("localhost:3306", "csgoxdne", "thisisasecretpassword");
if (mysqli -> error){ print ("Error connecting! Message: ".$mysqli->error); }
mysqli_set_charset($mysqli, 'utf8');
function fetchinfo($rowname, $tablename, $finder, $findervalue) {
if ($finder == "1") {
$query = "SELECT * FROM $tablename WHERE rowname = '$rowname'";
$result = mysqli_query($mysqli, $query);
} else {
$query = "SELECT * FROM $tablename WHERE `$finder`='$findervalue'";
if (!$query) {
die('Invalid query: ' . $mysqli->error);
}
$result = mysqli_query($mysqli, $query);
}
return $result;
}
?>
Oh and make sure the port number on your localhost is correct.
Also to go through the values of result you can use:
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_array($result)) {
#do things
}
}

PHP No database found

So it says no database selected, please help me as my hosting cant
<?php
$sitename = "http://devilgaming.phy.sx/csgopie.xyz/";
$link = #mysql_connect("localhost", "devilgam_csgopie", "ssb12boycek1");
$db_selected = mysql_select_db('devilgam_csgopie', $link);
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'") or die (mysql_error());
$row = mysql_fetch_assoc($result);
return $row[$rowname];
}
?>
Where is says
$result = mysql_query("SELECT $rowname FROM $tablename WHERE
`$finder`='$findervalue'") or die (mysql_error());
The mysql_fetch_assoc() function returns an associative array, row by row. To access the rows you would do:
while ($row = mysql_fetch_assoc($result)) {
echo $row[$rowname];
}
According situations when You install purchased script, or You are comfort with mysql_* functions, I'll not say to switch to mysqli or PDO. PHP 7 will force to switch to modern drivers to everyone (:
but for Your question, run this script and tell me result:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$sitename = "http://devilgaming.phy.sx/csgopie.xyz/";
$link = mysql_connect("localhost", "devilgam_csgopie", "ssb12boycek1") or die(mysql_error());
mysql_select_db('devilgam_csgopie', $link) or die(mysql_error());
mysql_query("SET NAMES utf8");
function fetchinfo($rowname, $tablename, $finder, $findervalue) {
$q = "SELECT $rowname FROM $tablename";
if($finder != 1) {
$q .= " WHERE `$finder`='$findervalue'";
}
$result = mysql_query($q) or die (mysql_error());
$row = mysql_fetch_assoc($result);
return $row[$rowname];
}
?>
Also go to administration panel of hosting create or modify user: devilgam_csgopie to have same username and password.
Because:
Access denied for user 'devilgam_csgopie'#'localhost' to database

Errors when adding data to a database with PHP

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)

php mysqli error on some browsers only

I am getting this error on only some browsers and I am not sure why. I am hoping this is a simple fix. It sounds like it should be. Here is the error and below that is the code.
warning : mysqli_fetch_array expects parameters 1 to mysqli_result, boolean given in /home/content/yada/html/myapp/main.php on line 71
By the way, this is line 71:
while($row = mysqli_fetch_array($result))
and below is the full code
$type = $_POST[type];
$user="theUser";
$password="thePassword";
$database="theDatabase";
$TABLE = "user";
#mysql_connect("mydb.com",$user,$password);
#mysql_select_db($database) or die("Unable to select database");
if($_POST[type]) {
$query = "UPDATE $TABLE
SET type = $type
WHERE fbId = $id";
if(mysql_query($query)) {
//echo "Settings saved successfully!";
} else {
echo ("MySQL Error: ".mysql_error());
}
}
$con=mysqli_connect('localhost',"$user","$password","$database");
// Check connection
if(mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM $TABLE WHERE fbID = $id");
while($row = mysqli_fetch_array($result)) {
$currentType = $row['type'];
//echo $currentType;
}
if ($result = mysqli_query($con, "SELECT * FROM $TABLE WHERE fbID = $id", MYSQLI_USE_RESULT)) {
//echo "True";
//mysqli_free_result($result);
}
Use
echo mysqli_error($con);
to show the error MySQL server gives when executing the SQL query on line 71. This will reveal what is wrong with the query.

Connect php with server

I am connecting to server with help of php for an android application.
Name of Database in phpmyadmin is "student" , name of table is "data" and fields are "Name" and "EmpId"
This is what I coded n php and getting the error on the "$output" part as undefined variable
Here is the code:
<?php
$connection = connectionserver ();
function connectionserver (){
$con = mysql_connect("localhost", "root", "") or die ("connection not found");
if($con)
echo "Connection Created" ,"<br>";
$database = mysql_select_db ("student1", $con);
if($database) echo "Database Connected" , "<br>";
return $con;
}
$result = mysql_query("select * from data");
while ($row = mysql_fetch_assoc($result))
{
$output [] = $row;
}
print json_encode($output);
mysql_close($connection);
?>
declare $output as array before the while
$output = array();
$undefined_array[] = 'something' will not trigger an E_NOTICE error. However it is good practice to initialize the variable.
The error comes from the line with json_encode, most likely because your query didn't return any result, didnt get into the while loop, thus $output[] was never executed.
You may try this:
<?php
$connection = connectionserver ();
function connectionserver (){
$con = #mysql_connect("localhost","root","");
if(!$con) die("Can't connect!!");
$var2 = #mysql_select_db("student1",$con);
if(!$var2)
die("<br>"."can't select dataBase");
$result = mysql_query("select * from data");
while ($row = mysql_fetch_assoc($result))
{
$output[] = $row;
}
print json_encode($output);
mysql_close($con);
}
?>
Try this,
echo connectionserver();
function connectionserver (){
$con = mysql_connect("localhost", "root", "") or die ("connection not found");
$database = mysql_select_db ("student1", $con);
$result = mysql_query("select * from data") or die(mysql_error());
$output = array();
while ($row = mysql_fetch_assoc($result))
{
$output[] = $row;
}
mysql_close($con);
return json_encode($output);
}

Categories