MySQL select all missing a record - php

I have added a new record to a table using phpMyAdmin
I then run select * within phpMyAdmin and it works listing all records.
However if run the same select * from a PHP page the new record is missing.
I am using PHP 5 and am not sure what is going on here. It used to work in PHP 4.
.php files run as PHP 5 Apache module mode.

Here are a few things you can investigate:
You could be connecting to a different database from the one you think you are (do you have test and production databases?).
You may have an error or typo in your SELECT statement. Copy and paste exactly the query that works into your code. Do not manually edit it afterwards.
You might select the row correctly but display it incorrectly so that it appears not to be there. Try checking how many rows there are in your result set.

Try put sql query to variable and then dump it to log or use var_dump. Then put query to phpMyAdmin and check errors.

Thanks for the answers but found out the answer now.
I had bookmarked a link to an older phpMyAdmin server which looked almost identical and allowed me to log in etc. My host should have forwarded me to the latest server.
I am now using the correct server. Hope this helps someone else.

Related

PHP & MySQL UPDATE 10 columns

I want to update customer data which means 10 different columns would be updated in one UPDATE SET WHERE statement but I keep getting a warning in NetBeans that says method length 26 ( allowed 20)
Can anyone tell me what the method length refers to and what another way is to update all those columns in one statement ?
This is an utterly ridiculous hint. You can disable it in
Tools->Options->Editor->Hints->PHP.
It won't disturb you again.
Netbeans is notorious for these informational messages which are really a matter of style preference rather than an actual limitation.
I like to create my SQL code directly using a tool like phpMyAdmin to verify the statement works then port it over to my code.
For what is worth, I suggest you download and install mysql workbench, it will serve as sandbox to test queries before anything else. This always works for me in any development situations

PHP/MYSQL issue - query search failing randomly

I have a PHP script designed to scrape data from websites. The script checks a locally-hosted mysql database each time it finds a new item to see whether or not that item has already been downloaded and already exists in the Mysql database. If it sees the item already exists in the database, it should ignore it and move on. This is the code I am using to do that:
$result = mysql_query("SELECT * FROM web_media WHERE sourceForum LIKE '%$ForumtoGrab%' AND titleThreadNum=$threadTitleExists");
if((!mysql_num_rows($result)) && (mysql_num_rows($result) !== FALSE))
{}
In other words, if it comes up with zero results, then that item is considered new. This script ran fine on my old hosting company for several months. I have recently moved to a new hosting provider, and I'm suddenly running into a very strange issue. Every 12 hours or so, the expression seems to randomly fail and the script finds a bunch of "new" data that already exists in the mysql database. I've tried running the query manually, and the code appears to have no problem finding the pre-existing entry no problem.
Does anyone have any idea what's going on here? I already checked with the hosting provider, and they say that the number of aborted Mysql connections we have is very low and isn't anything to worry about…so it doesn't seem like it's an issue with MySQL itself. I suspect it may be an issue with the mysql query?
Thanks
Try to check the MySQL error log for errors in your query.
PS: I hope you are preparing the $threadTitleExists for usage in a MySQL query (smth like (int)$threadTitleExists or mysqli_real_escape_string($threadTitleExists))

PHP & MYSQL: checking whether certain DB exists in the server, from a php code executing mysql command

I want my php code to check whether a certain DB exists, by means of running some sql query andd parsing the result.
What would be a nice way to do it?
Thanks
Gidi
Run SHOW DATABASES and loop over the results with PHP to check for the existence.
If it were only one database, you could also add a condition to the SQL query directly and simply check if it returned a result or not with PHP. This would avoid the loop.
As a simple solution, you could just use:
SHOW DATABASES LIKE <YOUR DB NAME>;
See the SHOW DATABASES Syntax manual page for more information.
I suppose you could connect to your server, and issue a show databases statement.
It'll get you a list of all database that you can access, on your server.
I suppose you could also connect to your server, and, then, call mysql_select_db() or mysqli::select_db(), to try connecting to your specific database.
If it doesn't exist, that function will most likely fail -- and return false.
Keeping it in code, you could do
mysql_connect('host','user','pass');
$dbExists=mysql_select_db('db_name');
Might be quicker too.

phppgadmin : How does it kick users out of postgres, so it can db_drop?

I've got one Posgresql database (I'm the owner) and I'd like to drop it and re-create it from a dump.
Problem is, there're a couple applications (two websites, rails and perl) that access the db regularly. So I get a "database is being accessed by other users" error.
I've read that one possibility is getting the pids of the processes involved and killing them individually. I'd like to do something cleaner, if possible.
Phppgadmin seems to do what I want: I am able to drop schemas using its web interface, even when the websites are on, without getting errors. So I'm investigating how its code works. However, I'm no PHP expert.
I'm trying to understand the phppgadmin code in order to see how it does it. I found out a line (257 in Schemas.php) where it says:
$data->dropSchema(...)
$data is a global variable and I could not find where it is defined.
Any pointers would be greatly appreciated.
First, find all current procesid's using your database:
SELECT usename, procpid FROM pg_stat_activity WHERE datname = current_database();
Second, kill the processes you don't want:
SELECT pg_terminate_backend(your_procpid);
This works as of version 8.4, otherwise pg_terminate_backend() is unknown and you have to kill the process at OS level.
To quickly drop all connections connected to a given database, this shortcut works nicely. Must run as superuser:
SELECT pg_terminate_backend(procpid) FROM pg_stat_activity WHERE datname='YourDB';
On more recent Postgres versions (at least 9.2+, likely earlier), the column names have changed and the query is:
SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname='YourDB';
Not sure about PostgreSQL but i think a possible solution would be to lock the table so other processes will fail when they try to access it.
See:
http://www.postgresql.org/docs/current/static/sql-lock.html

SQL query works on testing server but not live... what could be the difference?

SOLVED:
I wrote and tested a PHP script on the local server. (Nothing fancy, just 2 consecutive SQL inserts in the same database, but different tables).
Both servers run PHP5 & MYSQL 5.
On the local server, both queries are processed correctly.
On the live server, only the first query works, but not the second and I can't figure out why.
Here is the code:
$sql_login = "INSERT INTO logintbl
(...)
VALUES (...)";
$result_login = mysqli_query($this->connect, $sql_login);
# Fill contact details
$sql_contactD = "INSERT INTO contactDetails
(...)
VALUES (...)";
$result_contactD = mysqli_query($this->connect, $sql_contactD);
On my local server, both queries return true and the data are added in the database.
On my live server, the first query works as expected, but the second query fails without any error message.
Of course, the table structures are identical on both servers. Both tables are in the same database and the user has sufficient rights on the database.
Any clue on what could be wrong?
Edit 1: Permissions: Yes, the user has adequate permissions on both tables.
Edit 2: I am feeling very silly, but following James' advice of checking mysqli_error(), I found out that the production server was case sensitive in regards to table names, unlike my testing server, AND that it converted the original name of my table (contactDetails) to lowercase (contactdetails).
Thanks to all for your help.
have you checked permissions in your Production environment? Meaning have you verified that you have insert access to contactDetails?
Did you try to print $sql_contactD to a screen (or webpage), and then run the exact same query in the MySQL Query Browser?
Answering my own question because it looks like the only way to close it properly:
I am feeling very silly, but following
James' advice of checking
mysqli_error(), I found out that the
production server was case sensitive
in regards to table names, unlike my
testing server, AND that it converted
the original name of my table
(contactDetails) to lowercase
(contactdetails).

Categories