Selecting from a table sql - mysql

I am trying to select some rows from a table that I have in my database but I keep obtaining null $result. I think the problem is with my sql code.
The query is the following:
$result = mysqli_query($this->conexion, $sql);
The conexion is working fine I have checked so the problem is with $sql. My vareiable $sql is:
"SELECT * FROM `invent` WHERE `id_system` IN (500,504,502,498,499)"
My table is invent and I have a row named id_system, I have checked all the names and are correctly spelt and the column id_system contains integers and all ones of the query exist in the table.
I keep getting:
$result={"current_field":null,"field_count":null,"lengths":null,"num_rows":null,"type":null}
I don't know why is it not working, it might be because of the spelling in $sql.
Could someone help me?
Thanks in advance

I don't know if this will help, but this code might work for you:
SELECT 'id_system'
FROM 'invent'
WHERE 'id_system'=500
OR 'id_system'=504
And then you could add the rest of the values you need

Related

Retrieve data from a specific row

I'm trying to get data from a specific row from my database.
And I get the error:
Unknown column 'Apalm' in 'where clause'
This is my code:
$naam = $_GET['naam'];
$result = mysql_query("SELECT * FROM planten WHERE naam = $naam")
or die(mysql_error());
And the picture below shows my database.
It seems like the script thinks the row is called "Apalm". But I clearly stated to search in 'naam'?
This is probably very easy to fix, but I just can't seem to find it on Google.
So please help me, or point me in the right direction. I'm very eager to learn this!
Thanks in advance!
naam column seems to be a text, so enclose its value between single quotes within the sql code as well:
"SELECT * FROM planten WHERE naam = '$naam'"
However, pls consider using either proper escaping or prepared statements to prevent sql injection.

retrieving multiple columns from table using redbeanphp

I have a query which retrieves 3 columns from table which works fine in phpmyadmin
SELECT cab_id,
SUM(IF(rating=1,1,0)) as up,
SUM(IF(rating=0,1,0)) as down
FROM rating WHERE cab_id=101
table->'rating'
Can anyone help me to find how I can get this query worked using redbeanphp ?
I tried ,
R::getRow(),R::getAll(),R::$adapter->getAssoc()
none is working!!!
To check the correctness of code I tried,
SELECT * FROM rating group by cab_id having cab_id=101
and found working.But I need the first query statement to work! Any help ,please?
I got the answer finally, This one works.
$val = R::getAll($query);

MySQL only inserting first row

I'm trying to insert a ton of rows into my MySQL database. I have a query like this, but with about 700 more repetitive entries in it but for some reason the query is only inserting the first row to the database. In this case it would be '374','4957','0'.
INSERT INTO table VALUES ('374','4957','0'),('374','3834','0'),('374','4958','0'),('374','5076','0'),('374','4921','0'),('374','3835','0'),('374','4922','0'),('374','3836','0'),('374','3837','0'),('374','4879','0'),('374','3838','0')
I can't figure out what I'm doing wrong.
Thank you in advance.
Don't mean to state the obvious, but if the first field '374' is your primary key field, than this is the issue.
Otherwise, are there any error messages received from the database? That is always a good place to look for bugs.
For better understanding why something is not working next time use code like this:
$sql = "INSERT INTO table VALUES ('374','4957','0'),('374','3834','0')";
if (!mysqli_query($link, $sql)) {
printf("Errormessage: %s\n", mysqli_error($link));
}
That should display error message returned from MySQL.
More information: PHP manual - mysqli_error
Try to write the column names before values.
For example:
INSERT INTO table (column1,column2,column3) VALUES ...

Issue With a Mysql Query Displaying A columns Data

I am trying to run this query from a drop down menu, the category and the destination data are working fine, just not the subcategory, it's probably an issue with my Html or Javasscript but i wanted to get the opinion of an expert with MYSQL if poss? I am quite new to PHP and MYSQL, but learning fast, Not sure my HTML will post here so i will just ask if someone can confirm that my query should work ok. Thanks guys.
$category=$_POST['Category'];
$subcategory=$_POST['Subcategory'];
$destination=$_POST['Destination'];
$result = mysql_query("SELECT * FROM travel WHERE Category='$category' AND Subcategory='$subcategory' AND Destination='$destination'")
or die(mysql_error());
$row = mysql_fetch_array( $result ) ;
Not sure if it's a culprit in your case, but you have to escape data coming from a user before passing it to the query :
$category=mysql_real_escape_string($_POST['Category']);
$subcategory=mysql_real_escape_string($_POST['Subcategory']);
$destination=mysql_real_escape_string($_POST['Destination']);
....

UPDATE query not functioning in MySQL 5.0

I'm not completely sure what I'm doing wrong here I've looked through again and again for incorrectly placed quotes but I cannot for the life of me get this UPDATE query code to work... and I've troubleshooted it down to the query itself and it gives an incorrect syntax error, yet I have no idea what is wrong with it as it is identical to the manual.
$change = mysql_query("UPDATE Images SET Group='$group' WHERE ID = '$imgid'") or
die(mysql_error());
you might need to do :
"UPDATE Images SET `Group`='$group' WHERE ID = '$imgid'"
as group is a keyword in MySQL .
Well... if the update does not work this might have two reasons. Either your syntax is wrong; this should result in MySQL throwing an error which should show up with your die(mysql_error()).
Or there is simply no record matching your WHERE-condition. To check if any record was updated at all you might want to take a look at mysql_affected_rows(). And you could run a SELECT-query on that table, using the same WHERE-condition.
try this ..
"UPDATE Images SET Group='".$group."' WHERE ID = ".$imgid