Hey I'm writing a stored procedure in phpMyAdmin. I created one to search a database:
SELECT * FROM cars WHERE name LIKE CONCAT ('%', search ,'%');
The problem here is this: When I run it, it only returns 1 row, but it should return at least 3 when I input a search string. I also tried this part ('%', search ,'%'); with dots
('%'. search .'%'), but this gives me an SQL syntax error. What is the problem here? I run this procedure in phpMyAdmin.
Related
My database table contains an eloquent query
click here to see Database Table Snapshot
.I have fetched the query from database.Now problem is that I'm unable to execute the query that is in the collection.
I have Tried eval() to execute the query present the collection.But eval() is not executing it.
eval("\"$tempdata\";");
$tempdata contains the query that is in the database table.
You need to add return when using eval.
Try:
eval("return $tempdata;");
I' am using MySQLI Wrapper as stated in the docs about running rawQuery. Since it doesn't have any function to search database I had write my own. Below is the code that am using.
$songs = $db->ObjectBuilder()->rawQuery('SELECT * FROM songs WHERE song_name LIKE ?', array('%test%'));
The query returns empty results, yes there are data in the database which should match the search keyword. Any idea what am I doing wrong in the Query?
I don't know the structure and content of songs but your code seems to be correct. You can try if problem persists when array('%test%') is inserted into query by inserting this manually. Try to execute this:
$db->ObjectBuilder()->rawQuery("SELECT * FROM songs WHERE song_name LIKE '%test%'");
If this query returns empty results then I would suspect that something wrong is either in your query or in the database. Otherwise, there is something wrong with rewriting parameters by rawQuery function.
I have a single MySQL query created from PHP 5. The Query has 3 SELECT and 2 JOIN clauses. It accesses two databases on a single host using one connection and db1.table1 db2.table2 techniques. I echo the query before running. In PHP the query returns no result and does not error. When I copy and paste the echoed query into SQL in PHPMyAdmin, it returns the correct result.
Why is PHP different to the SQL part of PHPMyAdmin and does anyone have any suggestions about getting it to work in PHP?
Why is PHP different to the SQL part of PHPMyAdmin
It's not.
You're connecting to the wrong database, writing the wrong query, or checking for errors incorrectly.
Can the php function mysql_insert_id() return no result after processing the INSERT query in mysql db?
Just to clarify. There was a script performing by cron on the production site. It contained a cycle for generating invoices for users. Each iteration consists of a INSERT db query and the mysql_insert_id() operation going right after the query - to fetch the generated invoice number. A set of iterations were performed without fetching the last inserted number.
Can it be caused by high db server load or by some other reasons that are not linked to the problem at the php code site?
Any help would be appreciated!
Offhand, I can think of a few cases where MySQL wouldn't return the ID:
The table you're inserting into doesn't have an AUTO_INCREMENTed primary key.
You're inserting multiple rows at once.
You're calling mysql_insert_id() from a different connection than the INSERT query was executed.
The INSERT query didn't succeed (for instance, it encountered a deadlock). Make sure you are checking the return value from mysql_query(), then use mysql_errno() and mysql_error().
MySQL docs have a full list of conditions and details on how this function works.
Of course, it's also possible there is a bug in MySQL, which would depend on which version of MySQL you are using.
If you're running the commands through a shell script, and run them both separately as in;
mysql -e "insert into table ( field1 ) values ( 'val1' );" "database"
lastId=`mysql -e "select last_insert_id();" "database"`
Then that won't work as the second call makes a new connection to the server. You need to do something like the following, as it is all done within a single database call / connection;
lastId=`mysql -e "
insert into table ( field1 ) values ( 'val1' );
select last_insert_id();
" "database"`
You'll need to look up the extra parameters required for the MySQL command to remove formatting and header row - I'm afraid I can't remember them off the top of my head!
I have a simple script which inserts values from a text file into a mysqldatabase - however some accented characters aren't inserted properly. Like lets say I have a word:
Reykjavík
I try to insert it using a simple insert sql statement and instead I this value ends up in the database????
ReykjavÃk
How do I fix this?
====EDIT====
I tried to change the collation - the thing is that I'm using navicat here and if I lets say try to insert any such word with accentuated characters like this using the navicat QUery generator or panel it is inserted perfectly with no problem whatso ever - however my php script when it runs a similar query end s up putting trash in the table :( whats going on here...
The set of questions marks is because you are inserting a character whose encoding is not understood by MySQL. Try collating with utf8_bin. This should work.