How do I make my table visible to PhpStorm? - php

I am using PhpStorm; when I execute my code, the browser shows me:
you have created case
test
Table 'test.users' doesn't exist
But the table users does exist? How do I fix this?
My php code looks like this:
<?php
define('dbuser','root');
define('dbpass','');
define('dbserver','localhost');
define('dbname','test');
$conn = mysqli_connect(dbserver,dbuser,dbpass,dbname);
if(!$conn){
die('error connecting to database');
}
echo 'you have created case';
?>
<h1>test</h1>
<?php
$query = "SELECT * FROM users";
$result = $conn->query($query);
if (!$result) die($conn->error);
?>
And in my Microsoft SQL Server I have a database named test with table users with one user in it.

First you need to sort out the basics.
You seem to have your connection to the database but that is just like switching on the computer. Now you have to do the work.
So:
Are you using MySql or SQL Server?
You need to think/read a bit more. https://www.w3schools.com/php/php_mysql_intro.asp is a good place to start (if you are using MySQL!).
Make a cup of coffee and just BROWSE the mySql section (don't try and understand). Keep hitting the NEXT button and then circle back to look at the bits you need to know.
Have another try (experiment lots!)
Re-edit your question with what you have learned if you are still having problems!
And that's it!
This is not really an answer but it should help you get there.
One last very important thing: edit the question to something useful if you can. Stack Overflow can be very harsh on new users but if you make the effort to correct this then I will certainly upvote you to help you get started on Stack Overflow and hopefully a couple of forgiving souls will help you get back to zero.
My rule of thumb: I never post until I have done at least 30 mins research. Annoyingly, then when I then write the question, nine times out of ten I find the answer as I am editing it so never get to ask my "useful" question.
Keep trying. This place is immensely helpful but you do need to make the effort to get the best from it.

Related

How bad it is to copy paste my own code blocks?

! Actually, I am learning PHP from last couple of months and now I am in a stage where I can program small things like a simple Login Page in PHP and mySQL or a Contact Form. I have wrote a lot of codeblocks like inserting something into a database or selecting something from a database etc. etc. But, I always copy paste my own code-blocks from previous projects while working on a new one. So, I want to know whether this tendency is unique to me only or each of the beginner passes through the same phase once during their journey of being a developer?
Please bear with me because I know this isn't really a programming question and doesn't worth your time as well. I tried finding out in Google as well but this is a snap of what I found:
I mean to say that most of the search results dealt with copy pasting other's code which is not the case of what I am talking about. In order to save time I do copy paste my own code blocks almost everytime. So, how bad is this behaviour of mine?
I again apologize for not posting a question that is worth your time but I am finding it hard to learn to code by myself without having any mentor nearby ( Actually, I searched for a mentor who could teach PHP before giving it a start all by myself, but I found none in my area ) for clearing my doubts and as such Internet is the thing which I mostly depend upon for learning about anything.
This question probably belongs on https://softwareengineering.stackexchange.com but I'll try to give you a decent answer and some guidance.
People re-use their own code all the time. You do not however want to copy/paste if possible. The issue with copy/paste is when you have something used more than a few times - like a MySQL database connection - and it needs updating. I'd rather modify one file (or one small group of files) and have all of my webapps fixed/updated than having to modify 2 or 3 database calls in 9 different web apps...
For things that I use everywhere/all the time - talking with our course management systems API, authenticating a user against our LDAP server, connecting to a MySQL database and running queries, processing forms that are emailed, etc - I've built up my own (or coworkers have) sets of functions, classes, etc. Which I then keep in a single directory, and can include as needed.
If you do this, you want your functions/object methods to be as generic as possible - for example, my MySQL query function takes several arguments - an associative array with connection info (since we have several DB servers based on purpose), a query, and an array of parameters. It returns an array with a status code, and then appropriate data - the record set result for inserts, the ID of the last insert, the count of rows affected (for delete/update). This one function handles 50+ queries and connects to 4 different MySQL servers.

PHP, Access denied for user 'username'#'localhost' (using password: YES) on SECOND query

I'm trying to migrate an elder and quite simple PHP application to a new server with very limited success.
$conn = mysqli_connect($hostname, $username, $password, $database);
$query1 = "select field1 from table2 where anotherfield < 3";
$res1 = mysqli_query($conn, $query1);
$row_one = mysqli_fetch_assoc($res1);
// do something with result in $row_one and maybe loop through results
$query2 = "select field2 from table2 where anotherfield = 'somevalue'";
// the next one is going to fail
$res2 = mysqli_query($conn, $query2);
$row_two = mysqli_fetch_assoc($res2);
// do something with $res1 and $res2
The first mysqli_query runs fine. I get records and can do something on them. Then I need another result from another query to go on, but the second query to the same connection throws this "Access denied" error.
No problem as long as I do just ONE query.
You might guess that I previously had "mysql" instead of "mysqli" in this code, which is true. The weird thing is, that I still have the old code running on an old server which is fine connecting to its old MySQL. If I point it to the new MySQL (MariaDB) on the new server, the same old code shows the same "one query only" problem. I initially thought the old PHP 5 was not able to properly talk to the new MariaDB and PHP 7 will do, but it's exactly the same there.
My guess is with some quirks on the Database and not with the PHP part, as these consecutive queries SHOULD work. Should they still work?
I already checked for "max_connections" entries in the DB config file and in the user privileges without spotting something unusual, but maybe I just dont't know what to look for.
On the other hand, the very web/DB server runs other PHP/MySQL applications quite fine without any hickup. Everything but my piece of code.
I'm quite sure I miss something trivial and elementary.
Thanks to mario and Shadow, you guided me the right way. The certain knowledge that this kind of query definitely looks right and should work put my focus on the database again.
So here it goes...
It turned out that "table2" in the above example was not a table but a VIEW. No inherent problem with this, if not the original definition had been lazy and bad in the old database (before migration):
CREATE
ALGORITHM=UNDEFINED
DEFINER=`root`#``
SQL SECURITY DEFINER
VIEW `table2` AS ...
But there is no user "root#" in the database, it has never been and nobody could ever define one. We have "root#localhost" and we have "user#%".
The old MySQL obviously silently interpreted that as "root#%' or similar and the select went well. The new one picks the "SQL SECURITY DEFINER", looks up the privileges of "root#``" and correctly fails with "Access Denied".
I redesigned the view as the database owner with the correct "user#localhost" and additionally with "SQL SECURITY INVOKER", in case anyone else might open the query to make sure the table privileges are used.
There have been a handful of those bad views in between lots of correctly defined ones and I picked a bad one by chance as the "second" one each time.
Easy fix, once you know about it.

Add comments to mysql queries (php -> mysql-db)

I got a website with a lot functions and calculations and it grows every day. Calculation errors are getting harder to solve as my loggs are really big.
The website is used by employees, so there is a lot of traffic between the website and the mysql database. Is it possible in any way to append comments to the queries so they show up in the log-files?
The standart ones (I guess #..#) get lost after the query is parsed from the mysql-server.
Im just trying to get a better overview about my log-files.
For example it could be something like this:
$query = "UPDATE something SET column = 'input' WHERE ... #ticket-calc#";
$result = mysql_query($query, $db) or die(mysql_error());
I would like to show up 'ticket-calc' for this query in the log-files.
Everything I tried hasnt worked. It looks like the server is saving just the raw-command without anything appended.
I also thought about just comparing a string (the comment) with 'true' what shouldnt change the general query.
[query] ... AND 'ticket-calc'= true
I hope there is a more clean way to get it.
Solved this by using /* comment */. Thanks!
I have same approach to log (in MySQL) where are my queryes in database. I use "--" to solve the problem.
$pdo->exec("-- GETTING USERS");
$stmt->execute();
By the way, is better make all logs in your application, inclusive, log over querys. Aka
file_put_contents('TRY TO EXECUTE QUERY: ' . $stmt->queryString);
$stmt->execute();
As no one of the ppl who helped me out in the comments want to post their solutions as an answer, Ill do so to get this topic closed.
Im using /* .. */ to mark my queries.
Thanks for the help #AlexGreg, #davidkonrad and #davidkonrad.

Mystery ghost MySQL insert query

My MySQL db has inserted rows which should not be there, in a particular there is data in a column not generally used. I thought this would make it easy to find which PHP script was inserting the rows but i have searchd all insert querys for the entire site and cannot find which php script is running the insert query.
Its also very hard to replicate as this particular table has many crons updating it.
Can anyone please try point me in the right direction of how I might go about debugging this. Is there a stack track I can use to determine the originating php script. Because it hard to replicate and I've spend two days searching for the code causing the inserts Im open to suggestions.
Im normally quite good at debugging but this bug is like a ghost.
The only thing I can think of, if you have looked at all your code including in all your old cron scripts, is to put an insert trigger on that table, and use it to find out what time of day your extra rows get inserted.
Nasty problem!
This might be a horrible idea depending on your error reporting settings and what type of environment you are debugging in, but if you are in a position to have some errors thrown I'd remove write permissions for the column in question and wait to see what script throws the error. If you are lucky log/report for the error will be written to include a script name and line.

Optimizing and compressing mysql db with drupal

I have developed a news website in a local language(utf-8) which server average 28k users a day. The site has recently started to show much errors and slow down. I got a call from the host saying that the db is using almost 150GB of space. I believe its way too much for the db and think there something critically wrong however i cannot understand what it could be. The site is in Drupal and the db is Mysql(innoDb). Can any one give directions as to what i should do.
UPDATE: Seems like innoDb dump is using the space. What can be done about it? Whats the standard procedure to deal with this issue.
The question does not have enough info for a specific answer, maybe your code is writing the same data to the DB multiple times, maybe you are logging to the table and the logs have become very big, maybe somebody managed to get access to your site/DB and is misusing it.
You need to login to your database and check which table is taking the most space. Use SHOW TABLE STATUS (link) which will tell you the size of each table. Then manually check the data in the table to figure out what is wrong.

Categories