Creating a function to retrieve information from SQL - php

I've searched the site for an answer but couldn't find any answer for my specific problem that worked.
I have two files, the first one is my index.php of-course, the second is my functions.php file, which obviously contains the functions, I made this function:
function sql_get($lang, $tabler, $rower){
if ($lang == "heb") {
$result = mysql_query("SELECT * FROM %s WHERE id = 0",
mysql_real_escape_string($tabler));
$row = mysql_fetch_array($result);
return $row['$rower'];
}
else if ($lang == "rus") {
$result = mysql_query("SELECT * FROM %s WHERE id = 0",
mysql_real_escape_string($tabler));
$row = mysql_fetch_array($result);
return $row['$rower'];
}
this code supposed to get an information about the language (from a get, it gets it, it's all fine with that), the sql table and the specific row from this table where the id is 0.
and return the information from the row inserted.
My warnings and errors when the language is "heb":
Warning: mysql_query() expects parameter 2 to be resource, string given in /home/elenbyin/public_html/elenby.co.il/vadim/functions.php on line 16
Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in /home/elenbyin/public_html/elenby.co.il/vadim/functions.php on line 17
and when the language is "rus":
Warning: mysql_query() expects parameter 2 to be resource, string given in /home/elenbyin/public_html/elenby.co.il/vadim/functions.php on line 23
Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in /home/elenbyin/public_html/elenby.co.il/vadim/functions.php on line 24
the function call in the index.php file looks like that:
I will be thankful for the people who will try to help and especially for those who will help me.
Thanks A lot!

Firstly, you haven't connected to the database inside your function. If you've opened your database somewhere then you can get away with this with mysql, but you shouldn't.
Secondly, you're passing your variable as a second parameter to mysql_query(), but mysql_query() expects the query to be complete as the first parameter, and the second parameter should be the connection resource tht connects to the database, which you haven't got.
From the structure of the query it looks like you intend to use sprintf() to create it.
This should create the query for you, assuming you have opend a databse connection:
$query = sprintf("SELECT * FROM %s WHERE id = 0",
mysql_real_escape_string($tabler));
$result = mysql_query($query) or die(mysql_error());
Note: mysql is deprecated. You shouldn't use it. Use mysqli or PDO instead. You'll need to be more rigorous about passing in your database connecions if you use mysqli.

As Amal suggested - better not use mysql_* functions cause not only they're deprecated but also vulnerable to sql-injection.
That said, in order to fix your code you should do:
$table = mysql_real_escape_string($tabler);
$sql = "SELECT * FROM $table WHERE id = 0";
$link=mysql_connect('host','user','pass');
$result = mysql_query($sql, $link);
...
as the second (and optional) parameter of mysql_query should be a resource - not a string!

Related

Getting an odd result: mysqli_fetch_array() expects parameter 1 to be mysqli_result

I keep getting weird results from this query.
Not that I am not using PDA because this is just a prototype. In production I plan on tightening all of the screws and making it more secure.
include ('../includes/DBConnect.php'); //exactly how it is in other working files
$query = "SELECT * FROM CHARACTERS WHERE USER_ID=(SELECT ID FROM USERS WHERE EMAIL='"+$_SESSION['user']+"') ORDER BY id DESC"; //I have copy pasted this into mysql and it worked, switching the session variable with a string
I get an error with the this line
while($row = mysqli_fetch_array($character_list)){
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\Node2\public\main.php on line 36
I know this has to be stupid. I can't figure it out. I just looked at documentation and other files I have written that worked. And a few stack overflow threads to no avail.
Thank you so much.
You're using + instead of . to concatenate strings. Gotta remember this is PHP, not JavaScript ;)
$query = "SELECT * FROM CHARACTERS WHERE USER_ID=(SELECT ID FROM USERS WHERE EMAIL='".$_SESSION['user']."') ORDER BY id DESC";

Error boolean given in... MYSQL

Plz Help i Dont know what is wrong in this function ....
$gsql = "SELECT * FROM posts WHERE group='$group_name' ORDER BY postdate DESC LIMIT 0,20";
$gquery = mysqli_query($db_conx, $gsql);
$gstatusnumrows = mysqli_num_rows($gquery);
while ($grow = mysqli_fetch_array($gquery, MYSQLI_ASSOC)) {
and it keeps saying this error :-
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in D:\group.php on line 3
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in D:\group.php on line 5
That means your query failed.
[mysqli_query] returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.
So use mysqli_error to find out what you did wrong. In this case, though, it's because you have a column named "group". GROUP is a reserved word in MySQL. To be on the safe side, ALL database, table and column names SHOULD be enclosed in backticks ` to prevent any possible ambiguity.

mysql_free_result() expects parameter 1 to be resource

im sending data from my android application to mySQL in localhost and I receive warning on (Warning: mysql_free_result() expects parameter 1 to be resource, Boolean given in C:\xampp\htdocs\datatest.php on line 17)
despite the warning i'm still able insert the data into the database.
i'm wondering is it ok to ignore this or how can i solve this problem?
i tried various forums and website by none solve my problems.
<?php
$dbcnx = mysql_connect("localhost", "root", "");
$db = "agentdatabase";
mysql_select_db($db, $dbcnx);
$user_id=$_POST['username'];
$passwd=$_POST['password'];
$query = "INSERT INTO agentable (username,password) VALUES ('".$user_id."','".$passwd."')";
echo $query;
$result = mysql_query($query) or die ("<b>Query failed:</b> " . mysql_error());
if($result){
echo '<br />','pass';
}
else echo mysql_error();
mysql_free_result($result);
mysql_close($dbcnx);
?>
You can't free the result of an INSERT query, since you can't free a boolean.
Side note, the MySQL PHP extension is deprecated. It's better to use MySQLi or PDO.
mysql_free_result() only needs to be called if you are concerned about how much memory is being used for queries that return large result sets.
mysql extension is deprecated, instead use the MySQLi or PDO_MySQL.
mysql_query() only returns a resource for SELECT, SHOW, EXPLAIN, and DESCRIBE queries.
Here's another possible reason that I tested and was able to reproduce this error in two other different ways which have not been mentioned here.
1) Your SQL has the wrong syntax and looks like this (notice the incorrect extra comma after field3:
"select field1, field2, field3, from myTable";
2) Your SQL contains a duplicate field and looks like this:
"select field1, field2, field2 from myTable";
In both cases I got the message and it was displayed in different functions:
"Warning: mysql_result() expects parameter 1 to be resource, boolean given"
"Warning: mysql_free_result() expects parameter 1 to be resource, boolean given"
"Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given"
use this:
if (is_bool($result) === false) {
mysql_free_result($result);
}

mysql_fetch_assoc() expects parameter 1 to be resource, null given [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP Error: mysql_fetch_array() expects parameter 1 to be resource, boolean given
I am trying to build an array from a mysql object after querying.
The number or rows returned in the query is 68, this is why I thought a forloop would be good. However php does not like my code inside the forloop.
Here is the code:
$result = $db->query("SELECT * FROM `networktariff`");
$rows = mysql_num_rows($result);
for ($i= 0; $i<$rows; $i++)
{
$tariffs[$i] = mysql_fetch_assoc($result[$i]);
}
I am getting an error message saying :
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, null given in /usr/local/www/apache22/data/quote/index.php on line 58
There is no such thing like "mysql object".
Your $db object belongs to some user-defined class and there is no point in asking anyone about this class' behavior.
Assuming that query() method just utilizing ordinary mysql_query() function call, it does return a resource, not object.
If you have a strange idea of using some class to run the query yet bare API functions for the rest,
$tariffs = array();
$result = $db->query("SELECT * FROM `networktariff`");
while ($row = mysql_fetch_assoc($result) {
$tariffs[] = $row;
}
I dunno though why your class doesn't have some helper function to get the whole data at once, something like
$tariffs = $db->query2arr("SELECT * FROM `networktariff`");
Yup, read the docs on mysql_fetch_assoc().
A better construction might be:
while($row = mysql_fetch_assoc($result)) {
// Do stuff with $row
}
To elaborate: $result does not contain any data. It's a "resource" handle that points to a resultset that MySQL has created. To access the data of the resultset, you use mysql_fetch_* and pass it the resource handle. Each time you call a fetch function, the internal pointer is incremented one row. So you'll get fresh data each time until MySQL reaches the end of the results, at which point you'll get FALSE. This is why the while() loop works.
print $result as $db->query is not standard php function, it might return you result set array.
try same code with replacing $db->query to mysql_query

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in [duplicate]

This question already has answers here:
mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource
(31 answers)
Closed 8 months ago.
I get following Error:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in......
Here is my Query:
$query = "SELECT ListNumber FROM residential";
$result1 = mysql_query($query);
if (mysql_num_rows($result1) >10){
$difference = mysql_num_rows($result1) - 10;
$myQuery = "SELECT * FROM `residential` ORDER BY `id` LIMIT 10,". $difference;
$result2 = mysql_query($myQuery);
while ($line = mysql_fetch_array($result2, MYSQL_BOTH))
Your query ($myQuery) is failing and therefore not producing a query resource, but instead producing FALSE.
To reveal what your dynamically generated query looks like and reveal the errors, try this:
$result2 = mysql_query($myQuery) or die($myQuery."<br/><br/>".mysql_error());
The error message will guide you to the solution, which from your comment below is related to using ORDER BY on a field that doesn't exist in the table you're SELECTing from.
The code you have posted doesn't include a call to mysql_fetch_array(). However, what is most likely going wrong is that you are issuing a query that returns an error message, in which case the return value from the query function is false, and attempting to call mysql_fetch_array() on it doesn't work (because boolean false is not a mysql result object).
mysql_fetch_array() expects parameter 1 to be resource boolean given in php error on server if you get this error : please select all privileges on your server. u will get the answer..
This error comes when there is error in your query syntax check field names table name, mean check your query syntax.

Categories