I am having an array of fields and after using implode function and converting them to string, I am trying to use this string as names of columns in mysql_query() function as follows:
$field_array = array('course','batch','branch');
$fields = implode(", ",$field_array);
$resource = mysql_query("SELECT $fields FROM some_table") or die(mysql_error());
but I am getting the following error. What is that I am doing wrong here ?
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM fix_data' at line 1
Below is the exact code I am using
function fetch_resource_db_nowhere($table_name,$field_array,$return_type,$return_type_name) {
if($field_array[0]=='ALL') {
//echo "asda";
$resource = mysql_query("SELECT * FROM ".$table_name."") or die(mysql_error());
}
else {
$fields = implode(",",$field_array);
$sql = "SELECT ".$fields." FROM ".$table_name."";
echo $sql;
$resource = mysql_query($sql) or die(mysql_error());
}
if($return_type == 'resource') {
return $resource;
}
if($return_type == 'resource_array') {
return mysql_fetch_assoc($resource);
}
if($return_type == 'resource_array_value') {
$resource_array = mysql_fetch_assoc($resource);
return $resource_array[$return_type_name];
}
}
$data = fetch_resource_db_nowhere('fix_data',array('course','branch','name'),'resource','');
You were trying to implode an array called field_array, even though your example shows an array called fields_array:
$fields_array = array('course','batch','branch');
$fields = implode(", ",$fields_array);
$resource = mysql_query("SELECT $fields FROM some_table") or die(mysql_error());
Edit: You changed your code again. Could you please give us the exact code that you're working with?
Related
I'm not really much into PHP or mySQL so I hope you can help me.
I got a php script with a function that returns a json with all the entries in a database table:
public function select($table, $wheres = null)
{
$connect = $this->connect();
if ($wheres == null)
{
$query = mysqli_query($connect, 'SELECT * FROM `'.$table.'`');
} else {
$query = mysqli_query($connect, 'SELECT * FROM `'.$table.'` WHERE '.$this->wheres($wheres));
}
$i = 0;
$ret = array();
while ($row = mysqli_fetch_assoc($query)) {
foreach ($row as $key => $value) {
$ret[$i][$key] = $value;
}
$i++;
}
return ($ret);
}
Edit:
I have my application which calls this function. Everything worked as expected but then I added 2 more fields to one of the tables ( 1 text, 1 varchar ), and now when I call this function it returns nothing ; literally an empty string.
I've also noticed that if I just delete those new fields it works, which is kinda annoying plus I need those fields and I can't figure out where the problem root is.
Also, the application code is not the problem: when this function is called it only passes 2 parameters (the method name, and the table name), it can't be wrong.
By the way, here is the table structure if it may help you help me:
pic related
This function helps you to get data form database in json from , this return json
<?php
function getData($tbl_name=null,$id=null)
{
$con = mysqli_connect("localhost","root","","mydb"); //my db is my database name
if(!$con){ die( "could't connect with database" ); }
$sql = mysqli_query($con,'SELECT * FROM $tbl_name WHERE id = {"$id"}');
while($row = mysqli_fetch_assoc($sql))
{
#echo"<pre>";
#print_r($row); //this will return data form data base in array from
return json_encode($row);
}
}
?>
So I have this piece of code that is not returning anything (the echo returns nothing and should be returning two rows):
<?php
include "connection.php";
$cliente = $_POST["cliente"];
$select = "SELECT CLIENTE, NOMCLI FROM CLIX1 WHERE NOMCLI LIKE ? ORDER BY NOMCLI";
$stmt = odbc_prepare($con, $select);
//preparing the array for parameter
$prep_array = array();
$prep_array[] = "'%$cliente%'";
$rs = odbc_execute($stmt, $prep_array);
$nombres = array();
$clienteIDS = array();
//if prepare statement is successful
if($rs)
{
$i = 0;
while($row=odbc_fetch_array($stmt))
{
$cliente_id = trim($row["CLIENTE"]);
$nombre = utf8_encode(trim($row["NOMCLI"]));
$nombres[$i] = $nombre;
$clienteIDS[$i] = $cliente_id;
$i++;
}
echo json_encode($nombres) . "|" . json_encode($clienteIDS);
}
else
{
echo "error";
}
odbc_close($con);
?>
I know the problem is not the parameter pass on the odbc_execute() because even if I do this, it doesn't return anything(with %mich% it should display two rows):
$rs = odbc_execute($stmt, array("%mich%"));
Do you see anything wrong in this code?
Please let me know and thanks in advance.
UPDATE ------
I made the changes on the code that were suggested on the answer below and I am getting a new error now:
Warning: odbc_execute(): Can't open file %mich%
Where mich is the text entered to search on the database.
I found the following that may relate: ODBC prepared statements in PHP
$prep_array = array();
$prep_array[] = "'%$cliente%'";
$rs = odbc_execute($stmt, $prep_array);
I think the Double Quotes might be causing an issue.
These lines are from a php function on a web server, the client is an ios app,
I get an error on the result2 query
$result = query("SELECT field1 FROM table1 WHERE field2='%s' limit 1", $value);
$row = mysql_fetch_array($result);
$result2 = query("SELECT field2 FROM table1 WHERE field3='%s' limit 1", $row['field1']);
on Xcode I get the error (in json):
{
error = "The operation couldn't be completed. (Cocoa error 3840.)";
}
here is the definition of the function query
//executes a given sql query with the params and returns an array as result
function query() {
global $link;
$debug = false;
//get the sql query
$args = func_get_args();
$sql = array_shift($args);
//secure the input
for ($i=0;$i<count($args);$i++) {
$args[$i] = urldecode($args[$i]);
$args[$i] = mysqli_real_escape_string($link, $args[$i]);
}
//build the final query
$sql = vsprintf($sql, $args);
if ($debug) print $sql;
//execute and fetch the results
$result = mysqli_query($link, $sql);
if (mysqli_errno($link)==0 && $result) {
$rows = array();
if ($result!==true)
while ($d = mysqli_fetch_assoc($result)) {
array_push($rows,$d);
}
//return json
return array('result'=>$rows);
} else {
//error
return array('error'=>'Database error');
}
}
what's wrong with that query?
thanks
You are using mysqli_ functions in your query() function yet you are trying to use mysql_fetch_array() to get the results. You need to use: mysqli_fetch_array()
http://www.php.net/manual/en/mysqli-result.fetch-array.php
Actually it looks like your query() function does it for you. You shouldn't need to use a function at all. Look at this section of your query() function:
$rows = array();
if ($result!==true)
while ($d = mysqli_fetch_assoc($result)) {
array_push($rows,$d);
}
//return json
return array('result'=>$rows);
However not sure why it says it is json cause it is just a normal array not a json array. So you should just use this in your code:
$row = $result['result'][0];
This will get the first row.
maybe you have to json_encode the result?
//return json
return json_encode(array('result'=>$rows));
So I have a function to gather user_data in PHP & MYSQL, and the thing is that I want to upgrade MYSQL in MYSQLi.
The MYSQL code is following:
$data = mysql_fetch_assoc(mysql_query("SELECT $fields FROM members where id = $id"));
The MYSQLi code I tried but with no use:
$data = $db_connect->query("SELECT $fields FROM ´members´ where id = $id");
and
$result = $db_connect->query("SELECT $fields FROM ´members´ where id = $id");
$data = $result->fetch_assoc();
I don't know what could be wrong, in the 1:st example I have no errors but the data isn't displaying, and in the 2:nd code I noticed I need the fetch_assoc function to make it work, but here I get the errors saying
Call to a member function fetch_assoc() on a non-object
Seems like you have an error in your query. MySQli->query() will return FALSE on failure.
[UPDATE 2] Try this code:
$result = $db_connect->query("SELECT $fields FROM members where id = $id");
if (!$result) {
printf("Errormessage: %s\n", $db_connect->error);
}
else {
while ($data = $result->fetch_assoc()) {
print_r ($data);
}
}
I am using the JQuery Validation Plugin. I got the remote function working with the default php file.
I modified the php file to use my own version but mysql is returning
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/fastbluf/syatch/module/1.func.php on line 15
My PHP Code is the following. All my syntax looks correct.
<?php
// Last Edited: 4/23/12
$conn = mysql_connect('localhost','hidden','hidden') or die('Iam dying');
$rs = #mysql_select_db( "hidden", $conn) or die( "Err:Db" );
$do = $_REQUEST['do'];
$email= $_REQUEST['email'];
$user= $_REQUEST['user'];
function checkInfo($do,$email,$user){
switch ($do) {
case 1:
$sql = "select * from User_Base where Email_Address = $email";
$results = mysql_query($sql). mysql_error();
$nResults = mysql_num_rows($results);
if ($nResults > 0) {
$valid="false";
} else {
$valid="true";
}
break;
case 2:
//not yet
break;
}
return $valid;
}
echo checkInfo($do,$email,$user);
?>
The problem is that you're appending to your result, causing it to no longer be a valid result.
$results = mysql_query($sql). mysql_error();
Try changing this to be something like this:
$results = mysql_query($sql) or die(mysql_error());
Your query should also be changed to quote the email address, and the address should be escaped to prevent attacks (SQL Injection):
$email = mysql_real_escape_string($_REQUEST['email']);
$sql = "select * from User_Base where Email_Address = '$email'";
Fix your query to
$sql = "select * from User_Base where Email_Address = '".$email."'";