Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'd like to have a function that creates database-tables:
input:
$tableName
$tableFields (parameters array)
output: new DB TABLE
The two dimension array contains the fields detentions:
a[0][0] = firstName
a[0][1] = varchar[25]
a[1][0] = lastName
a[1][1] = varchar[30]
The output is a table named $tableName with two fields firstName and lastName.
I think that I can build a create query by looping the array, yet I don't know how to tell php to commit this command and actually create the DB table.
function createTable( $tableName,$a) {
global $con; //database connection
$query = "CREATE TABLE $tableName ";
$temp = "";
for($i=0;$i<count($a);$i++) {
$temp .= $a[$i][0] . " " . $a[$i][1] . ",";
}
if($temp!='') {
$query .= substr($temp,0,-1);
mysqli_query($con,$query);
}
else {
print "No column names provided";
}
}
just have a look into the mysqli module of PHP: http://www.php.net/manual/de/book.mysqli.php
There you can create a connection to a database, perform queries etc.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
$result = mysqli_query($connection,"SELECT * FROM libsutdent where libid='$_POST[libid]'");
$rowcount=mysqli_num_rows($result);
if($rowcount==1)
{
while($row = mysqli_fetch_array($result))
{
$libid=$row['libid'];
$regno= $row['regno'] ;
$name= $row['stuname'] ;
$branch= $row['branch'] ;
$semester= $row['semester'] ;
$section= $row['section'] ;
$yearofadm= $row['yearofadm'];
}
}
Dont post anything directly in database as its a threat to data security (SQL Injection)
$libid = $_POST['libid'];
$libid = mysqli_real_escape_string($connection, $libid);
$result = mysqli_query($connection,"SELECT * FROM libsutdent where libid='".$libid."'");
Make sure that your mysql field is really libsutdent. Seems like it should be libstudent.
Then Place {} around your Post variable. ie {$_POST[\"libid\"]}.
Conversely you can place another step in your code like:
$libid = $_POST["libid"];
I think you can do without the quotes around libid, but I always think it reads better to add them.
$result = mysqli_query($connection,"SELECT * FROM libsutdent where libid='$_POST[libid]'");
Should be
$result = mysqli_query($connection,"SELECT * FROM libsutdent where libid='".mysql_real_escape_string($_POST['libid'])."'");
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I made a new table , everything worked.
CREATE TABLE IF NOT EXISTS logdata (
email varchar(30),
password varchar(20),
username varchar(15),)
Inserted the id auto increment code
,and some data :
INSERT INTO logdata(email,password,username,id) VALUES('test#test.org','testtest1','test',' ')
Everything worked here. When I try to output the data i dont get any results (except "ERROR"). I have no idea why.
<?php
error_reporting(E_ALL);
// here is where I set the connection , everything is working here
if(mysqli_connect_errno()){
echo "Could not connect to the database <br /><br />";
echo mysqli_connect_error();
exit();
}
$dostuff="SELECT * FROM logdata";
$query = mysqli_query($db_conn, $dostuff);
if($query == TRUE) {
echo "Succes!";
}
else{
echo "ERROR ";
echo mysqli_error($db_conn);
}
?>
In order to query something in your database, you have to provide a query to it. Your query variable is an empty string!!
$dostuff="";
It should have some SQL statements, like e.g:
$dostuff="SELECT * FROM logdata";
Or whatever.
UPDATE
I believe that using === to test the result will fail because the mysqli_query returns a mysql_result object, according to the docs:
Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.
So if its succeful it won't be === TURE for your SELECT statement and it will have no error. Your query is fine, just try this:
if ($query = mysqli_query($db_conn, $dostuff)) {
echo "Success!";
}
else {
echo "ERROR ";
echo mysqli_error($db_conn);
}
It should works.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I am trying to select a table and I don't know what I am doing wrong:
$result = mysql_query('SELECT * FROM sfat WHERE done="0" LIMIT 0,10');
$row = mysql_fetch_array($result)
$url = $row["web"];
use
while ($row = mysql_fetch_array($result) ) {
$url = $row["web"];
echo $url;
}
in stead of
$row = mysql_fetch_array($result)
$url = $row["web"];
Because your query indicates you are expecting up to 10 rows. But your code will only show the first one.
The following example will be used to create database
<?php
$con=mysqli_connect("hostname","username","password");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Create database
$sql="CREATE DATABASE my_db";
if (mysqli_query($con,$sql))
{
echo "Database my_db created successfully";
}
else
{
echo "Error creating database: " . mysqli_error($con);
}
?>
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
if (mysqli_connect_errno()==0)
{ // if successfully connected
$sent_dt=$_POST['scts'];
// important: escape string values
$txt=mysqli_real_escape_string($con, $_POST['text']);
$snd=mysqli_real_escape_string($con, $_POST['sender']);
// creating an sql statement to insert the message into the SMS_IN table
$sql="INSERT INTO SMS_IN(studentID,lastname,firstname,class) VALUES ('34','Lekan','Balogun','Grade8')";
// executing the sql statement
$insert_sms_success = mysqli_query($con,$sql);
// closing the connection
mysqli_close($con);
}
Please, I'm new to php and I got this code from a friend for my project. I want to split the text message using spaces (e.g 34 Lekan Balogun Grade8) before its inserted into my database. My table fields are: StudentID, LastName, FirstName, Class and so I will want it posted as 34 for ID, Lekan for Lastname, Balogun for Firstname and Grade8 entered into class. Thanks in anticipation.
You can use explode() funnction in php for this
$str = '34 Lekan Balogun Grade8';
$arr = explode(' ',$str);
echo '<pre>';
print_r($arr);
You can split the string values using "explode" function,
$textArray = #explode(" ",$txt);
echo "<pre>";
print_r($textArray);
echo "</pre>";
This is you complete code
if (mysqli_connect_errno()==0)
{ // if successfully connected
$sent_dt=$_POST['scts'];
// important: escape string values
$txt=mysqli_real_escape_string($con, $_POST['text']);
$snd=mysqli_real_escape_string($con, $_POST['sender']);
$arr = explode(' ',$txt);
echo '<pre>';
print_r($arr);
$StudentID = $arr['0'];
$LastName = $arr['1'];
$FirstName = $arr['2'];
$Class = $arr['3']
// creating an sql statement to insert the message into the SMS_IN table
$sql="INSERT INTO SMS_IN(sms_text,sender_number,sent_dt) VALUES ('$txt','$snd','$sent_dt')";
// executing the sql statement
$insert_sms_success = mysqli_query($con,$sql);
// closing the connection
mysqli_close($con);
}
?>
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In my database table, I have a lot of questions ,each having a category . Say, there are 100 questions and some number of categories which I don't know . I want to know the number and the names of the categories.
Please tell the way to do it in php.
You can try something like this
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Perform queries
$result = mysqli_query($con,"SELECT category_name FROM your_table GROUP BY category_name");
$count = mysqli_num_rows($result); // no of categories
while ($row=mysqli_fetch_row($result)) {
echo $row[0]; // printing category name
}
How about...
<?php
$mysqli = mysqli_connect( /* info */) or die ("MySQL error");
$result = mysqli_query("SELECT COUNT(DISTINCT `category`)");
echo $result;
?>
That is assuming categories are all in a single column.