在php中执行MySQL时未选择数据库 [英] No Database selected when executing MySQL in php

查看:102
本文介绍了在php中执行MySQL时未选择数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用此php代码时遇到问题,该php代码需要在用户指定的数据库中创建一个表.但是,每当我尝试执行SQL时,它都会告诉我没有选择数据库.我的代码如下

I'm having problems with this php code which needs to create a table in the database specified by the user. But whenever I try to execute the SQL it tells me no database selected. My code is as follow

<?php  
$con = mysql_connect("127.0.0.1","peter")
or die('Error connecting to mysql'); // Check connection  
// Create table 
mysql_select_db("USE Ebooks");//Select Database

$foldername = mysql_real_escape_string($_POST['foldername']);//Obtain Folder Name 

$sql = sprintf("CREATE TABLE %s (ID CHAR(3) ,Books CHAR(30))", $foldername);  
mysql_query($sql) or die(mysql_error()); 
mysql_close($con);  
?>

推荐答案

使用

mysql_select_db( "Ebooks" ) or die( 'Error'. mysql_error() );

这篇关于在php中执行MySQL时未选择数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆