PHP Mysql script error - php

I send data too this script from a C# script inside of Unity 3d . What I want to do is get the page to display the result of whatever query i end up running from it , and inside of Unity I get the text of that page and display it .
This is a very jerryridged way of doing this , but i'm just starting with PHP and mysql
Here's the error that unity 3d gives me
"Warning: mysql_result(): supplied argument is not a valid MySQL result resource in WEBSITE.com/newget.php on line 30"
<?php
$db = mysql_connect('host, user, pass') or die('Could not connect: ' . mysql_error());
mysql_select_db('dbname') or die('Could not select database');
$gotString = mysql_real_escape_string($_GET['GetString'], $db);
$hash = $_GET['hash'];
//SELECT * FROM table_name
$real_hash = md5($gotString . $secretKey);
$secretKey = "KeyHERE";
$real_hash = md5($gotString . $secretKey);
$locString = "SELECT A FROM Quiz1 WHERE Question = 1";
if ($real_hash == $hash) {
Compare();
}
function Compare() {
if ($gotString == "1B") {
$result = mysql_query("SELECT B FROM Quiz1 WHERE Question = 1") or die(mysql_error());
} else {
$result = mysql_query("SELECT A FROM Quiz1 WHERE Question = 1") or die(mysql_error());
}
}
print( $result);
?>

Try use
if (!$result) {
die('error: ' . mysql_error());
} else {
$row = mysql_fetch_array($result);
var_dump($row);
}
instead of Print( $result) ; at the end

Related

trying to do SELECT in php but i get

So I got a nifty little bit of code set to run along and find me information when I give it a certain KittenID but its not working at all, I am sad. And oh so tired, Can anyone tell me where I have gone wrong? and yes I do have:
<?php
date_default_timezone_set('America/New_York');
//If statements:
//find:
date_default_timezone_set('America/New_York');
if(isset($_POST['Find']))
{
$connection = mysql_connect("ocelot.aul.fiu.edu","userName","password");
// Check connection
if (!$connection)
{
echo "Connection failed: " . mysql_connect_error();
}
else
{
//select a database
$dbName="spr15_xgotz001";
$db_selected = mysql_select_db($dbName, $connection);
//confirm connection to database
if (!$db_selected)
{
die ('Can\'t use $dbName : ' . mysql_error());
}
else
{
$result = mysql_query($connection,"SELECT * FROM Kittenz WHERE KittenID =<?php$_POST[KittenID]?>;)
while($row = mysql_fetch_array($result))
{
$Name = $row['Name'];
$KittenID = $row['KittenID'];
$KittenAge = $row['KittenAge'];
$Email = $row['Email'];
$Comments = $row['Comments'];
$Gender = $row['Gender'];
$Personality = $row['Personality'];
$Activity = $row['Activity'];
echo $row['Comments'];
}
}
}
mysql_close($connection);
}
?>
Use
$result = mysql_query($connection,"SELECT * FROM Kittenz WHERE KittenID = " .$_POST['KittenID']);
instead of
$result = mysql_query($connection,"SELECT * FROM Kittenz WHERE KittenID =<?php$_POST[KittenID]?>;)
Note: Please use mysqli_ for your future projects
You need to privide more context. How are you setting the $_GET['id'].. is it in fact being stored as $_GET['KittenID'] (e.g. https://yoursite.com?view&KittenID=1). If so...
You can set a variable and declare the 'KittenID'
$kittenid = $_POST['KittenID'];
$result = mysql_query($connection,"SELECT * FROM Kittenz WHERE KittenID = $kittenid");
I suggest providing more context. What error are you getting? What do your parameters look like?
Use
$result = mysql_query($connection,"SELECT * FROM Kittenz WHERE KittenID = " .$_SERVER['KittenID']);
instead of
$result = mysql_query($connection,"SELECT * FROM Kittenz WHERE KittenID =<?php$_POST[KittenID]?>;)

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.

Why won't this query run via php?

I have:
$con = mysql_connect($db_server,$db_username,$db_password);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($db_db, $con);
mysql_query("INSERT INTO failed_logins SET username = 'pachonk', ip_address = INET_ATON('$ip'), attempted = CURRENT_TIMESTAMP");
// mysql_close($con);
echo $ip;
$count = get_attempts(); // Get the Number of Attempts
mysql_close($con);
sleep((2 ^ intval($count)) - 1);
function get_attempts(){
$result = mysql_query("SELECT * FROM failed_logins WHERE ip_address = INET_ATON('$ip')");
if(mysql_num_rows($result) > 0)
{
$num_rows = mysql_num_rows($result);
return $num_rows;
}
else
{
echo NO;
return 0;
}
}
And when I run the second query on my SQL server, it runs just how I want it to, however I get 0 lines when I query via php. If I query for "username = pachonk", It's perfect how I want though as well. What's going on?
If $ip is not empty(in your example $ip is empty) you can try to modify your function get_attempts() in get_attempts($ip);
your insert query should more or less look like this:
mysql_query("INSERT INTO failed_logins values('pachonk', INET_ATON('$ip'), CURRENT_TIMESTAMP");
then your select will give you some result

PHP error with mysql_num_rows

I have a simple form which when submitted should count the number of rows in a table which already have the same value as that submitted.
I can't work out why this is returning an error... any ideas?
The error is Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in .../register.php on line 31
$con = mysql_connect("table","user","pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db", $con);
function check_input($value, $quoteIt)
{
// Stripslashes
if (get_magic_quotes_gpc())
{
$value = stripslashes($value);
}
// Quote if not a number
if (is_null($value) || $value=="") {
$value = 'NULL';
} else if (!is_numeric($value) && $quoteIt == 1) {
$value = "'" . mysql_real_escape_string($value) . "'";
}
return $value;
}
$useremail = check_input($_POST['useremail'], 1);
// Check to see if email address already exists in USERS table
$query="SELECT * FROM users WHERE email = $useremail";
$result = mysql_query($query);
echo $query;
echo mysql_num_rows($result); //THIS IS LINE 31
mysql_close();
You can see your query error using this, because of query error only this error showing
$result = mysql_query($query) or die(mysql_error());
<?php
$con = mysql_connect("table","user","pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db", $con);
function check_input($value, $quoteIt)
{
// Stripslashes
if (get_magic_quotes_gpc())
{
$value = stripslashes($value);
}
// Quote if not a number
if (is_null($value) || $value=="") {
$value = 'NULL';
} else if (!is_numeric($value) && $quoteIt == 1) {
$value = "'" . mysql_real_escape_string($value) . "'";
}
return $value;
}
$useremail = check_input($_POST['useremail'], 1);
// Check to see if email address already exists in USERS table
$query="SELECT COUNT(1) FROM users WHERE email = $useremail";
$result = mysql_query($query);
$array = mysql_fetch_array($result);
$whatYouWant = $array[0][0];
mysql_close();
?>
But Leigh is right, prefer PDO instead of mysql_* functions...
Usually this means the query is wrong. Try backquotes for fields:
"SELECT * FROM `users` WHERE `email` = '$useremail'"
Replace
$result = mysql_query($query);
with
$result = mysql_query($query, $con);

Categories